向DataGridView添加一行 [英] add a row to DataGridView

查看:164
本文介绍了向DataGridView添加一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式向DataGridView添加一行(数据源为
与它绑定),但是当我调用方法DataGridView.Rows.Add时,会出现一个
System.InvalidOperationException将被抛出,错误消息是:
行不能以编程方式添加到DataGridView的行中
控件绑定数据时进行收集.


I want to programmatically add a row to DataGridView (a datasource was
binded with it), but when I call the method DataGridView.Rows.Add, a
System.InvalidOperationException will be thrown and the error message is:
Rows cannot be programmatically added to the DataGridView''s rows
collection when the control is data-bound.


private void add_click(object sender, EventArgs e)
{
 try
 {
    DataRow row = db1.NewRow();
    dataGridView2.Rows.Add(dataGridView2.CurrentRow.Index);
    sda.Update(db1);
 }
 catch (Exception exceptionObj)
 {
   System.Windows.Forms.MessageBox.Show(exceptionObj.Message.ToString());
 }

}





What code change should be done to dynamically add a row to datagrid when it is bound to data source.?

推荐答案

您需要将新行添加到而不是DataGridView.


这篇关于向DataGridView添加一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆