在 DataGridView 中获取用户添加的行 [英] Getting user added row in DataGridView

查看:21
本文介绍了在 DataGridView 中获取用户添加的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下绑定到列表的 DataGridView:

I have the following DataGridView which is bound to a List:

List<Object> list = new List<Object>();
System.Windows.Forms.DataGridView dataGridView1;
dataGridView1.DataSource = new BindingSource(list, null);
dataGridView1.UserAddedRow += new System.Windows.Forms.DataGridViewRowEventHandler(this.dataGridView1_UserAddedRow);

void dataGridViewMilight_UserAddedRow(object sender, DataGridViewRowEventArgs e) {

    // e.Row.DataBoundItem is always null?
}

如何获取已添加到我的列表中的对象?

How can I get the Object that was added to my list?

推荐答案

我遇到了一个与 DataGridView 绑定到 DataView 的类似问题.我想在创建 DataRow 时为隐藏文件提供默认值,但从来没有任何绑定项目.

I had a similar issue with a DataGridView bound to a DataView. I wanted to give default values to hidden fileds when a DataRow was created but there was never any bound item.

我通过处理由 DataView 引发的 ListChanged 事件解决了这个问题(当事件的 ListChangedType 属性等于 ItemAdded 时).如果使用 BindingList 而不是列表或任何实现 IBindingList 的类,则可以处理相同的事件.

I fixed the problem by handling the ListChanged event raised by the DataView (when the ListChangedType property of the event equals to ItemAdded). You can handle the same event if you use a BindingList instead of a list, or any class implementing IBindingList.

这篇关于在 DataGridView 中获取用户添加的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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