Hi Chetan,
I didn’t quite the problem you are facing. Based on my understanding about your post, you want a datagrid with two columns. The first column only accepts numbers, and the second column accepts all characters. Right? If so, I would like the following way to do this:
Code Snippet
DataGridP
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
DataTable dt = new DataTable();
private void Form3_Load(object sender, EventArgs e)
{
dt.Columns.Add("numbers");
dt.Columns.Add("any");
DataGridTableStyle ts = new DataGridTableStyle();
ts.GridColumnStyles.Clear();
DataGridTextBoxColumn tb1 = new DataGridTextBoxColumn();
tb1.HeaderText = "Numbers";
tb1.MappingName = "numbers";
tb1.Width = 120;
tb1.NullText = "";
tb1.TextBox.KeyPress += new KeyPressEventHandler(TextBox_KeyPress);
//you can also add validation event for first column here
ts.GridColumnStyles.Add(tb1);
DataGridTextBoxColumn tb2 = new DataGridTextBoxColumn();
tb2.HeaderText = "Any";
tb2.MappingName = "any";
tb2.Width = 120;
tb2.NullText = "";
ts.GridColumnStyles.Add(tb2);
this.dataGrid1.TableStyles.Add(ts);
this.dataGrid1.DataSource = dt;
}
void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar))
e.Handled = true;
}
}
I assume you use .Net Framework 1.1, because you told us you use DataGrid. If you use DataGridView in .Net Framework 2.0, these is an EditingControlShowing event for this control, and you can handle this event to get the TextBox which is hosted in this control.
Hope this helps.
Best regards.
|