How to programmatically add a row to a datagridview when it is data-bound?

喜夏-厌秋 提交于 2019-12-29 08:42:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!