我有一个Windows Form
dgv。现在它只是用于演示目的,所以我使用虚拟数据来填充一行数据。我还有一个名为的按钮,每次单击时Add New Row
只需添加新的空行。dataGridView
对于 dgv 的初始填充,我使用如下load
形式的事件:
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
MyData td13 = new MyData();
td13.colorCombinationColumn = "aff";
td13.remarksColumn = "abcdddd";
td13.prodColumn = true;
td13.LastModifiedColumn = "ab";
td13.ByColumn = "abc";
MyData[] arr = new MyData[2];
arr[0] = td13;
dataGridView1.DataSource = arr;
}
MyData
这堂课在哪里:
public class MyData
{
private int mId;
private string mCombination;
private string mremarks;
private bool mprod;
private string mLastModified;
private string mBefore;
public int Id
{
get
{
return mId;
}
set
{
...
到目前为止一切顺利,但后来我添加了我的按钮并处理他的click
事件:
private void btnAddRow_Click(object sender, EventArgs e)
{
SoleData[] arr = new SoleData[1];
dataGridView1.DataSource = arr;
}
这就是问题所在。如果我只是dataGridView1.Rows.Add(1);
说Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
。如您所见,我尝试了其他一些方法,但到目前为止没有成功。