简体   繁体   English

控件数据绑定时无法在DataGridView中以编程方式添加行

[英]Rows cannot be added programmatically in the DataGridView when the control data-bound

In my DatagridView already have data, then i want to add new row when click add button in windows form i got "Rows cannot add programmatically to the datagridview row collection when in the controls data-bound.在我的 DatagridView 中已经有数据,然后我想在 Windows 窗体中单击添加按钮时添加新行我得到“当在控件数据绑定中时,行无法以编程方式添加到 datagridview 行集合。

Then i google it i got some answer like you can use DataTable to add a new row, below code i used然后我谷歌它我得到了一些答案,比如你可以使用 DataTable 添加一个新行,下面是我使用的代码

Then, i tried to run,然后,我试着跑,

in my datagrid shows rows added but with blank values for all rows like a empty row在我的数据网格中显示添加的行,但所有行的值都为空值,如空行

kindly suggest me where i'm wrong i'm new at windows application datagridview with c#请告诉我我错在哪里我是 Windows 应用程序 datagridview 的新手,使用 c#

DataTable dataTable=new DataTable();

 foreach (DataGridViewColumn col in dataGridView.Columns)
 {
     dataTable.Column.add(col.Name);
 }
 foreach (dataGridViewRow row in dataGridView.Rows)
 {
     DataRow drow=dataTable.NewRow();
     foreach (DataGridViewCell cell in row.cells)
     {
        drow[cell.ColumnIndes]=cell.value;
        dataTable.Rows.add(drow);
     }
 }
   //then,
   //i assigned dataTable values to dataGridView

   dataGridView.DataSource=dataTable;
 }

I couldn't get why you do all of these things.我不明白你为什么要做所有这些事情。 You said you have data in your dataGridView, Mean you have already set headers of the dataGridView and you just want to add new row while clicking on the button.您说您的 dataGridView 中有数据,这意味着您已经设置了 dataGridView 的标题,并且您只想在单击按钮时添加新行。 If my perception is right then you just do the below thing on button click event.如果我的看法是正确的,那么您只需对按钮单击事件执行以下操作即可。

dataGridView1.Rows.Add("Column1", "Column2",..); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当控件是数据绑定的时,不能以编程方式将行添加到DataGridView的rows集合中 - Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound 当控件为数据绑定时,无法以编程方式将行添加到 datagridview 的行集合中 - Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound 当控件为数据绑定时,无法以编程方式添加到 DataGridView 的行集合中 - Cannot be programmatically added to the DataGridView's rows collection when the control is data-bound 当控件是集合数据源上的数据绑定错误时,不能以编程方式将行添加到DataGridView的rows集合中 - Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound error on collection data source 错误“当控件绑定数据时,行无法以编程方式添加到datagridview的行集合中” - Error “Rows cannot be programatically added to datagridview's row collection when the control is data-bound” XML加载后,DataGridView无法添加行,因为“控件绑定了数据” - DataGridView cannot add rows as “control is data-bound” after XML load 如何在数据绑定时以编程方式向 datagridview 添加一行? - How to programmatically add a row to a datagridview when it is data-bound? 无法将行动态添加到我的数据绑定dataGridview中 - Cannot add rows to my data-bound dataGridview dynamically 无法在数据绑定的 DataGridView 控件上设置 ColumnCount 属性。 c# - ColumnCount property cannot be set on a data-bound DataGridView control. c# c# ms access 数据库错误ColumnCount 属性不能在数据绑定的DataGridView 控件上设置 - c# ms access database error ColumnCount property cannot be set on a data-bound DataGridView control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM