C# FormatException in DataGridView

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"); } }


Handle DataGridView.CellValidating event:

private void CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { try { // get current cell var cell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex]; // check new value. i need any number >= 5 var c0 = 0; if (e.FormattedValue == null || !Int32.TryParse(e.FormattedValue.ToString(), out c0) || c0 < 5) { // bad value inserted // e.FormattedValue - is new value // cell.Value - contains 'old' value // choose any: cell.Value = cell.Value; // this way we return 'old' value e.Cancel = true; // this way we make user not leave the cell until he pastes the value we expect } } catch (Exception ex) { } }


  • The column 'ID' was specified multiple times for 'PivotTable'
  • Accessing a TableRow's style data in a TableView
  • SqlBulkCopy - Importing XML data into SQL table
  • How to Delete a Row or Disable Delete Button for specific row in GridView?
  • Dynamically setting MaxLength property of a DataColumn with DataAdapter
  • Replace value in DataColumn
  • T-SQL generated from LINQ to SQL is missing a where clause
  • Icefaces ace:dataTable lazy loading
  • Reading Excel files in a locale independent way
  • How can I draw two material charts with the Google Charts API without one being empty?
  • Primefaces Dialog Framework — dialogReturn event from menuitem
  • Catch change event on input field dynamically added to Jquery Datatables table
  • Delete suddenly taking a long time
  • Sorting is not working in jQuery datatables
  • Primeng - Cant display array items in a column
  • JSF disable inputbox with a checkbox in a ui:repeat on a composite component
  • JSF binding with setValueExpression read-only?
  • Sorting a DataTable using LINQ, when sort-by-columns may vary
  • Displaying image retrieved from database to image control
  • How do I generate data for Google Visualizations on the server using WebSharper
  • use images instead of text in webgrid asp mvc
  • How do I write an item to a DynamoDb with the AWS DynamoDB DocumentClient?
  • Serverless Framework Dynamo DB Table Resource Definition with Sort Key
  • Gforce min not supported for character in data.table
  • Adding Parent and Child Nodes in TreeView from Sql Server 2008
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • Can't delete or rename original file after resizing
  • c# open webrowser in many tab
  • Breaking out column by groups in Pandas
  • Unable to get column index with table.getColumn method using custom table Model
  • Retrieving value from sql ExecuteScalar()
  • AT Commands to Send SMS not working in Windows 8.1
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Angular 2 constructor injection vs direct access
  • How do I configure my settings file to work with unit tests?
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Binding checkboxes to object values in AngularJs
  • UserPrincipal.Current returns apppool on IIS