DataGridView绑定list对象,出现索引 0 没有值

已成功测试,用bindinglist而且不用重新绑定,好用!

在我做项目的时候,datagridview绑定对象列表后,再点击datagridview控件,就会报一个"索引 -1 没有值。"的错误信息,跟踪错误信息,发现是在System.Windows.Forms.CurrencyManager.get_Item(Int32 index)里出的错,在网上搜了一下,找到了以下文章。最后,我的解决办法是,在绑定数据之前,检查对象列表是否为0,如果是,则添一个空对象到对象列表,问题解决。

我是这样绑定DataGridVeiw的:
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客 IList<Resource> resources = new List<Resource> ();
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客Resource  resource 
= new
 Resource();
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客resources.Add(resource);
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客dataGridView.DataSource 
= resources;
    
        在第一个模块中能正常使用,没有任何问题。但到了第二个模块,同样的语句,只要一点击DataGridView控件,马上就会跳出System.IndexOutOfRangeException异常,具体内容如下:
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客 未处理 System.IndexOutOfRangeException
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客  Message
="索引 -1 没有值。"

DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客  Source
="System.Windows.Forms"
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客  StackTrace:
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.CurrencyManager.get_Current()
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell
&  dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti, Boolean isShiftDown, Boolean isControlDown)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Control.WmMouseDown(Message
&
 m, MouseButtons button, Int32 clicks)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Control.WndProc(Message
&
 m)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.DataGridView.WndProc(Message
&
 m)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message
&
 m)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message
&
 m)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG
&
 msg)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Windows.Forms.Application.Run(Form mainForm)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 KoalaStudio.ksRBAC.PrivilegeConfigTool.Program.Main()
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客       在 System.Threading.ThreadHelper.ThreadStart()
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客

        而且比较可恶的是,抛出异常的地方竟然是在Program.cs里,指向
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客 Application.Run(new FrmMain());
        这让人怎么调试,试了N回,也没发现原因。

        于是上网找了找,竟然真有解决的方法,其作者也并未解决这个问题,而是采取了折衷的方法,用了一个过渡组件来解决:
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客 BindingSource bindingSource = new  BindingSource();
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客bindingSource.DataSource 
=
 resources;
DataGridView绑定list对象,出现索引 -1 没有值。 - kogu - kogu的博客dgvResource.DataSource 
= bindingSource;

        问题可以解决,但又有了新的问题,因为使用了BindingSource,因此对与DataGridView的更改并不能同步到Ilist<Resource>对象上,所以还是不能用这种方法,继续研究。

         上国外的网站看了看,终于找到了问题的原因,在向DataGridView绑定Ilist<T>类型的对象是,如果对象的成员为0,那么就会出现
此问题。而且即使重新绑定DataGridView的数据源,也会继续存在此问题,解决的方法就是在向DataGridView绑定Ilist<T>对象是,要保证其中至少有一个成员,否则宁可不绑定(虽然不是最完美的解决方法)。
        其实最好的方法,是用BindingList<T>对象代替Ilist<T>对象作为DataGridView的数据源,即可彻底解决此问题,而且能实现DataGridView修改时与数据源的自动更新。但现在没有时间,只能先这样用了
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要利用BindingList来实现DataGridView绑定List对象的增删查改,可以按照以下步骤进行: 1. 定义一个实体类,用于存储数据。例如: ```csharp public class User { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } ``` 2. 在窗体中定义一个BindingList对象,并初始化为空。例如: ```csharp private BindingList<User> userList = new BindingList<User>(); ``` 3. 将BindingList对象绑定DataGridView上。例如: ```csharp dataGridView1.DataSource = userList; ``` 4. 实现添加功能。例如: ```csharp private void btnAdd_Click(object sender, EventArgs e) { User user = new User(); user.Id = userList.Count + 1; user.Name = txtName.Text.Trim(); user.Age = int.Parse(txtAge.Text.Trim()); userList.Add(user); } ``` 5. 实现删除功能。例如: ```csharp private void btnDelete_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { int index = dataGridView1.SelectedRows[0].Index; userList.RemoveAt(index); } } ``` 6. 实现修改功能。例如: ```csharp private void btnUpdate_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { int index = dataGridView1.SelectedRows[0].Index; User user = userList[index]; user.Name = txtName.Text.Trim(); user.Age = int.Parse(txtAge.Text.Trim()); dataGridView1.Refresh(); } } ``` 7. 实现查询功能。例如: ```csharp private void btnQuery_Click(object sender, EventArgs e) { string name = txtQueryName.Text.Trim(); var queryList = userList.Where(u => u.Name.Contains(name)).ToList(); dataGridView1.DataSource = queryList; } ``` 通过以上步骤,就可以利用BindingList来实现DataGridView绑定List对象的增删查改了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值