I created a DataGridView with some columns. The order columns only allow users enter int number. It throws the FormatException when I enter "j" (for example) and I try to add try catch to fix the problem, but it looks does not work..
private void Form1_Load(object sender, EventArgs e)
{
try{
this.sourceTable = new DataTable(TableName);
this.sourceTable.Columns.Add(new DataColumn(OrderCol, Type.GetType("System.Int32")));
dataGridView1.DataSource = sourceTable;
}catch(FormatException){
MessageBox.Show("Please enter a number");
}
}
Try this: I've added an event for column changing where I can check the input when it's submitted.
private DataColumn dataColumn;
private void Form1_Load(object sender, EventArgs e)
{
this.sourceTable = new DataTable(TableName);
dataColumn = new DataColumn(OrderCol);
this.sourceTable.Columns.Add(dataColumn);
sourceTable.ColumnChanged += sourceTable_ColumnChanged; // Eventhandler for column changes
dataGridView1.DataSource = sourceTable;
}
void sourceTable_ColumnChanged(object sender, DataColumnChangeEventArgs e)
{
try
{
int i = Convert.ToInt32(e.ProposedValue);
}
catch (FormatException)
{
MessageBox.Show("Please enter a number");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With