行不能以编程方式添加到DataGridView的行集合时,该控件是数据绑定 [英] Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound

查看:249
本文介绍了行不能以编程方式添加到DataGridView的行集合时,该控件是数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我抬起头,在的>但解决的办法 dataGridView1.Rows.Add()不在我的情况下工作。

First of all, I looked up this related question in here but the solution dataGridView1.Rows.Add() doesn't work in my case.

在我的DataGridView,我有数据输入3文本框和2组合框为用户选择的值(其结合到数据库中)。我的一个文本框设置为只读,这样用户只能填充数据网格外(与正常TexBox和一个按钮)。

In my Datagridview, I have 3 TextBoxes for data input and 2 ComboBoxes for the user to choose the values (which are bound into the database). One of my TextBoxes is set to read only so that the users can only populate it outside the datagrid (with a normal TexBox and a Button).

当用户填写一个DataGridView的数据,总有一个空行的底部;所以我禁用该功能,我用这个代码,以防止用户添加数据网格中新的行...

When the users fill a DataGridView with data, there is always an empty row at the bottom; so I disable this and I used this code to prevent the users from adding a new row inside the datagrid...

dataGridView1.AllowUserToAddRows = false

我只是想添加一个新行,当用户点击我上面提到的按钮(这将引发错误)。

I only want to add a new row when the users click the button I mentioned above (which throws an error).

我得到的错误信息是:

行不能被编程方式添加当控制是数据绑定DataGridView中的行集

"Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound"


中的一个具有红色箭头是组合框,以及一个绿色箭头只读文本框

the one with a red arrow is a ComboBox, and the one with green arrow is a read only TextBox

EDIT1:最后我发现这个解决方案。:D非常感谢球员......在NEWROW的方法完美的作品
但是我得到了另外一个问题,当我分配按钮来添加值到新的一行,整个小区将在编辑模式下,直到我点击其他单元格或标签,直到行末,那么它就会结束编辑模式。即时通讯使用 dataGridView.BeginEdit(真); 所以,如果我插入另一个值并按下按钮,新的值将取代先前插入的旧值。我试图用 dataGridView.EndEdit(); ,但它不结束编辑模式:(没有人知道如何解决这个

Finally I found the solution for this :D thanks a lot guys... the "NewRow" method works perfectly. But I got another problem, when I assign the button to add the value into a new row, the entire cell will be on edit mode, until I clicked on other cell or tabbed till the end of the row, then it will end the edit mode. im using dataGridView.BeginEdit(true); So, if I insert another value and press the button, the new value will replace the old value that was inserted previously. I was trying to use dataGridView.EndEdit(); but it doesn't end the edit mode :( does anyone know how to solve this?

推荐答案

看来好像你正在使用的DataGridView的DataSource属性。当该属性用于绑定到你不能明确地添加行数据直接到DataGridView。相反,您必须添加行directy到数据源。

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,使用被分配给数据源的数据表物业(未经测试):

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);
}

这篇关于行不能以编程方式添加到DataGridView的行集合时,该控件是数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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