keijikk
1/22/2016 - 10:11 AM

DataGridViewで、数値だけの入力

DataGridViewで、数値だけの入力



            dataGridViewValueColNo.Rows.Add(false, "日時");
            dataGridViewValueColNo.Rows.Add( false, "緯度" );
            dataGridViewValueColNo.Rows.Add( false, "経度" );
            dataGridViewValueColNo.Rows.Add( false, "標高" );
        /// <summary>
        /// http://stackoverflow.com/questions/12645458/make-a-specific-column-only-accept-numeric-value-in-datagridview-in-keypress-eve
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridViewValueColNo_EditingControlShowing( object sender, DataGridViewEditingControlShowingEventArgs e )
        {
            e.Control.KeyPress -= new KeyPressEventHandler( Column1_KeyPress );
            if ( dataGridViewValueColNo.CurrentCell.ColumnIndex == 0 ) //Desired Column
            {
                TextBox tb = e.Control as TextBox;
                if ( tb != null )
                {
                    tb.KeyPress += new KeyPressEventHandler( Column1_KeyPress );
                }
            }

        }
        private void Column1_KeyPress( object sender, KeyPressEventArgs e )
        {
            if ( !char.IsControl( e.KeyChar ) && !char.IsDigit( e.KeyChar ) )
            {
                e.Handled = true;
            }
        }