当我删除一个记录时,WinForms上的DataGridView会抛出异常 [英] DataGridView on WinForms throws exception when I delete a record
问题描述
System.IndexOutOfRangeException:索引3没有值 p>
我认为我的约束也有点跛脚。所以也许有人可以在这里给我一个指针。
public Form1()
{
InitializeComponent ();
empGrid.DataSource = stub.GetAllEmplyees();
empGrid.Columns [FirstName]。Visible = true;
StatusStrip.Text =员工管理;
}
我想做的是删除记录,然后刷新DataGridGridView。什么是定义哪些属性要显示在列中的最佳方法?
非常感谢!
我这样做,不使用DataSource,因为我不得不自定义单元格输出。
// for insert
foreach(var item in data)
{
DataGridViewRow newRow = new DataGridViewRow();
newRow.CreateCells(myDataGridView,
your,
data,
for,
each,
cell,
here);
myDataGridView.Rows.Add(newRow);
}
//更新
myDataGridView.Rows [rowIndex]
.SetValues(单元格,数据,你,愿望,到,更改,这里);
为了删除,我没有遇到任何问题:
myDataGridView.Rows.RemoveAt(rowIndex);
myDataGridView.Refresh();
应该工作刷新。
I have not done a lot with WinForms so I wonder if someone could give me a little assistance with this. I have a DataGridView that is bond to a IList<>. When I delete a selected record from the collection (ILIST<>) I get the following exception:
"System.IndexOutOfRangeException:Index 3 does not have a value"
I think my binding is a little lame too. So maybe someone can give me a pointer here as well.
public Form1()
{
InitializeComponent();
empGrid.DataSource = stub.GetAllEmplyees();
empGrid.Columns["FirstName"].Visible = true;
StatusStrip.Text = "Employee Administration";
}
What I would like to do is Delete a record and then refresh the DataGridGridView. What is the best way to define which properties you want to display in the columns?
Thanks a lot!
I do it this way, not using a DataSource as I had to customise the cell outputs.
// for inserts
foreach (var item in data)
{
DataGridViewRow newRow = new DataGridViewRow();
newRow.CreateCells(myDataGridView,
your,
data,
for,
each,
cell,
here);
myDataGridView.Rows.Add(newRow);
}
// for updates
myDataGridView.Rows[rowIndex]
.SetValues(cell,data,you,wish,to,change,here);
For deleting, I've encountered no problems using:
myDataGridView.Rows.RemoveAt(rowIndex);
myDataGridView.Refresh();
should work for refreshing.
这篇关于当我删除一个记录时,WinForms上的DataGridView会抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!