C# – How to programmatically add a row to a datagridview when it is data-bound

cdatagridview

How can I add a row to a datagridview control if it is bounded to a datasource (datatable) ? Thanks!

Best Answer

Add a row to the datatable, the datagridview will update automatically:

DataTable dt = myDataGridView.DataSource as DataTable;
//Create the new row
DataRow row = dt.NewRow();

//Populate the row with data

//Add the row to data table
dt.Rows.Add(row);