Столбец TextBox в DataGridView - PullRequest
       5

Столбец TextBox в DataGridView

5 голосов
/ 27 сентября 2011

Я добавил TextBox элемент управления в сетке: я хочу, чтобы мой столбец DataGridView TextBox содержал числа без десятичных значений. Как я могу это сделать?

Ответы [ 3 ]

3 голосов
/ 27 сентября 2011

From: http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/919b059c-dba9-40d2-bac7-608a9b120336

Вы можете обработать событие DataGridView.EditingControlShowing, чтобы привести элемент управления для редактирования к TextBox при редактировании в столбце, для которого вы хотите ограничить ввод, и присоединить событие KeyPress кTextBox, в функции обработчика событий KeyPress, мы можем вызвать метод char.IsNumber () для ограничения ввода с клавиатуры, что-то вроде этого:

private void Form1_Load(object sender, EventArgs e)
{
   DataTable dt = new DataTable();
   dt.Columns.Add("c1", typeof(int));
   dt.Columns.Add("c2");
   for (int j = 0; j < 10; j++)
   {
      dt.Rows.Add(j, "aaa" + j.ToString());
   }

   this.dataGridView1.DataSource = dt;
   this.dataGridView1.EditingControlShowing +=
      new DataGridViewEditingControlShowingEventHandler(
         dataGridView1_EditingControlShowing);
}

private bool IsHandleAdded;

void dataGridView1_EditingControlShowing(object sender,
    DataGridViewEditingControlShowingEventArgs e)
{
   if (!IsHandleAdded &&
       this.dataGridView1.CurrentCell.ColumnIndex == 0)
   {
      TextBox tx = e.Control as TextBox;
      if (tx != null)
      {
         tx.KeyPress += new KeyPressEventHandler(tx_KeyPress);
         this.IsHandleAdded = true;
      }
   }
}

void tx_KeyPress(object sender, KeyPressEventArgs e)
{
   if (!(char.IsNumber(e.KeyChar) || e.KeyChar == '\b'))
   {
      e.Handled = true;
   }
}
1 голос
/ 27 сентября 2011
public Form1()
{
   InitializeComponent();
   dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;
}

private void dataGridView1_EditingControlShowing(object sender , DataGridViewEditingControlShowingEventArgs e)
{
   // Convert the editing control to a TextBox to register for KeyPress event
   TextBox txt_edit = e.Control as TextBox;
   if(txt_edit != null)
   {
      // Remove any existing handler
      txt_edit.KeyPress -= TextBoxKeyPressed;
      // Add the new handler
      txt_edit.KeyPress += TextBoxKeyPressed;
   }
}

private void TextBoxKeyPressed(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
   // Test for numeric value or backspace in first column
   // Change this to whatever column you only want digits for
   if (dataGridView1.CurrentCell.ColumnIndex == 0)
   {
      // If its a numeric or backspace, display it.  Not a numeric, ignore it
      e.Handled = (!Char.IsDigit(e.KeyChar) && (e.KeyChar != (char)Keys.Back));
   }
}
0 голосов
/ 27 сентября 2011

Попробуйте это:

Convert.ToInt32(textBox1.Text)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...