当我删除一个记录时,WinForms上的DataGridView会抛出异常 [英] DataGridView on WinForms throws exception when I delete a record

查看:106
本文介绍了当我删除一个记录时,WinForms上的DataGridView会抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有做过很多WinForms,所以我想知道有没有人可以给我一点帮助。我有一个DataGridView绑定到IList<>。当我从集合(ILIST<>)中删除选定的记录时,我得到以下异常:



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屋!

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