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

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

问题描述

首先,我在这里,但解决方案 dataGridView1.Rows.Add()在我的情况下不起作用。



在我的Datagridview中,我有3个文本框用于数据输入,2个ComboBox用于用户选择值(绑定到数据库中)。我的一个TextBoxes被设置为只读,以便用户只能在datagrid之外(使用普通的TexBox和Button)填充它。



当用户填写DataGridView带有数据,底部总是有一个空行;所以我禁用这个,我使用这个代码来阻止用户在datagrid中添加一个新行...

  dataGridView1。 AllowUserToAddRows = false 

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



我收到的错误消息是:


无法以编程方式添加行到控件数据绑定到datagridview的行集合



一个带有红色箭头的是一个ComboBox,而带有绿色箭头的一个是只读TextBox


$ b $编辑1:最后我找到了解决方案:感谢很多人...NewRow方法工作得很好。
但是我有另一个问题,当我分配按钮将值添加到新行中时,整个单元格将处于编辑模式,直到我单击其他单元格或标签,直到该行的末尾,那么它将结束编辑模式。 im使用 dataGridView.BeginEdit(true); 所以,如果我插入另一个值并按下按钮,新值将替换以前插入的旧值。我试图使用 dataGridView.EndEdit(); ,但它不会结束编辑模式:(有没有人知道如何解决这个问题?

解决方案

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



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

  private void AddARow(DataTable table)
{
//使用NewRow使用
//表的模式创建一个DataRow的方法
DataRow newRow = table.NewRow();

//将行添加到rows集合
table.Rows.Add(newRow);
}


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

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).

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).

The error message I got was:

"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: 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?

解决方案

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.

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天全站免登陆