问题
How can I add a row to a datagridview control if it is bounded to a datasource (datatable) ? Thanks!
回答1:
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);
回答2:
- Create a class that corresponds to the data you want to display in your grid (same properties)
- In your data-binding function, get your query result, but put the result in a list (or any IEnumerable that suits you and the data binding)
- Create and add another object to your list according to your needs
- Bind the list
来源:https://stackoverflow.com/questions/18125147/how-to-programmatically-add-a-row-to-a-datagridview-when-it-is-data-bound