且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当控件为数据绑定时,无法以编程方式将行添加到 datagridview 的行集合中

更新时间:2023-01-05 10:25:21

看起来好像您正在使用 DataGridView 的 DataSource 属性.当此属性用于绑定到数据时,您不能直接将行显式添加到 DataGridView.您必须改为将行直接添加到数据源.

It appears as though you are using the DataSource property of the DataGridView. When this property is used to bind to data you cannot explicitly add rows directly to the DataGridView. You must instead add rows directy to your data source.

例如,如果您的数据源是 DataTable,则使用分配给 DataSource 属性的 DataTable(未测试):

For example if your data source is a DataTable, using the DataTable that is assigned to the DataSource property (untested):

private void AddARow(DataTable table)
{
    // Use the NewRow method to create a DataRow with 
    // the table's schema.
    DataRow newRow = table.NewRow();

    // Add the row to the rows collection.
    table.Rows.Add(newRow);
}