I have a grid view with template columns and in the ItemTemplate i have textbox bounded with sqldatasource, I want make this text box in Row 3 only to type only number and on the other Rows to type normaly any thing ?
Asked
Active
Viewed 503 times
1 Answers
0
- In EditingControlShowing, check that if the current cell lies in the desired column.
- Register a new event of KeyPress in EditingControlShowing(if above condition is true).
- Remove any KeyPress event added previously in EditingControlShowing.
In KeyPress event, check that if key is not digit then cancel the input.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress); if (dataGridView1.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; } }
Code Coutesy-Make a specific column only accept numeric value in datagridview in Keypress event
-
Thank you , but that make all the column, but i want to make one test box on that column on the 3rd row ? – KAS May 25 '15 at 19:32
-
Did you changed ColumnIndex == **[Desired column No]** and then you can writeanother IFLoop to find textbox of your choice . OR as second option you can use RowDataBound event of gridview. – v2v2 May 26 '15 at 09:26