C# 使用 DataGridView 进行 WinForms 数据绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14360637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WinForms DataBinding with DataGridView
提问by Adam Goss
I though I would post this as after spending several hours trying to work it out I am getting nowhere. Firstly, I am fully aware that databinding in WinForms is not the best. That said it does work in most scenarios.
我虽然我会发布这个,因为在花了几个小时试图解决它之后我一无所获。首先,我完全意识到 WinForms 中的数据绑定并不是最好的。也就是说它在大多数情况下都有效。
In my scenario, I have a binding source which is the master for my form. The object that is used for this binding source has a few simple properties and two binding lists as properties as well. Both this class, and the class type for the binding lists implement INotifyPropertyChanged. On my form, I have two DataGridViews for displaying the contents of the binding list properties.
在我的场景中,我有一个绑定源,它是我的表单的主控。用于此绑定源的对象也有一些简单的属性和两个绑定列表作为属性。此类和绑定列表的类类型都实现了 INotifyPropertyChanged。在我的表单上,我有两个 DataGridViews 用于显示绑定列表属性的内容。
This is also done through databinding at design time. I have two binding sources for each which use the main binding source as there data source and then the respective bindinglist properties as the data member.
这也是在设计时通过数据绑定完成的。我有两个绑定源,每个绑定源使用主绑定源作为数据源,然后使用各自的 bindinglist 属性作为数据成员。
So far, I would consider this to be fairly standard.
到目前为止,我认为这是相当标准的。
To update what is in these lists I have buttons to show a form that creates a new item, which I then add to the lists using BindingList.Add().
为了更新这些列表中的内容,我有按钮来显示一个创建新项目的表单,然后我使用 BindingList.Add() 将其添加到列表中。
Now in code, if you debug, the items are in the lists, however, the grids are not updating. But if I add a listbox to the form which uses just one of the list binding sources then both of the grids start refreshing as expected.
现在在代码中,如果您进行调试,项目在列表中,但是,网格不会更新。但是,如果我向仅使用其中一个列表绑定源的表单添加一个列表框,那么两个网格都会按预期开始刷新。
I apologise if any of this is unclear, I have tried to explain as best as I can with a confusing situation.
如果有任何不清楚的地方,我深表歉意,我已尝试在混乱的情况下尽我所能解释。
Any thoughts would be helpful as I really don't want to have to use a hidden list box.
任何想法都会有所帮助,因为我真的不想使用隐藏的列表框。
采纳答案by alexb
This code works fine for me
这段代码对我来说很好用
BindingList<Foo> source; // = ...
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = new BindingSource { DataSource = source };
this.dataGridView2.DataSource = new BindingSource { DataSource = source, DataMember = "Children" };
}
private void button1_Click(object sender, EventArgs e)
{
source.Add(new Foo { X = Guid.NewGuid().ToString() });
}
private void button2_Click(object sender, EventArgs e)
{
source[0].Children.Add(new FooChild { Y = Guid.NewGuid().ToString() });
}
with the model
与模型
public class Foo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
string x;
public string X
{
get { return x; }
set
{
x = value;
this.NotifyPropertyChanged();
}
}
BindingList<FooChild> children;
public BindingList<FooChild> Children
{
get { return children; }
set
{
children = value;
this.NotifyPropertyChanged();
}
}
}
public class FooChild : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
string y;
public string Y
{
get { return y; }
set
{
y = value;
this.NotifyPropertyChanged();
}
}
}
Both grids get refreshed.
两个网格都会刷新。
I hope this helps you
我希望这可以帮助你
Edit
编辑
I changed the Form1_Loadimpl
我改变了Form1_Loadimpl