winForms + DataGridView 绑定到 List(视觉工作室 2005、C# 2.0)
我有以下代码,但 DatGridView 显示空行。我找不到解决方案。我已将 AutoGenerateColumns
设置为 false
。如果我将其设置为 true
,它会创建行,但不允许我将此属性设置为 true
(客户希望)。
我做错了什么?这是代码:
public partial class Form1 : Form
{
private List<AStruct> _aCollectionList;
private BindingList<AStruct> _aCollectionBindingList;
public struct AStruct
{
public string ACode
{
get { return _aCode; }
set { _aCode = value; }
}
public string AName
{
get { return _aName; }
set { _aName = value; }
}
private string _aCode;
private string _aName;
}
public Form1()
{
InitializeComponent();
this.dataGridView1.AutoGenerateColumns = false;
}
private void button1_Click(object sender, EventArgs e)
{
_aCollectionList = new List<AStruct>();
FillCollectionStruct(true);
dataGridView1.DataSource = _aList;
dataGridView1.Refresh();
}
private void button2_Click(object sender, EventArgs e)
{
_aCollectionBindingList = new BindingList<AStruct>();
FillCollectionStruct(false);
dataGridView1.DataSource = _aBindingList;
dataGridView1.Refresh();
}
private void FillCollectionStruct(bool listBool)
{
AStruct aStruct = new AStruct();
for (int i = 0; i < 10; i++)
{
aStruct.ACode = i.ToString();
aStruct.AName = i.ToString();
if (listBool)
_aCollectionList.Add(aStruct);
else
_aCollectionBindingList.Add(aStruct);
}
}
}
在 Form1.Designer 中,我确实创建了以下列:
//
// aCodeColumn
//
this.colorCodeColumn.DataPropertyName = "Code";
this.colorCodeColumn.HeaderText = "a code";
this.colorCodeColumn.Name = "aCodeColumn";
//
// aNameColumn
//
this.colorNameColumn.DataPropertyName = "Name";
this.colorNameColumn.HeaderText = "a name";
this.colorNameColumn.Name = "aNameColumn";
I have the following code but the DatGridView is showing me empty rows. I can't find the solution. I have set the AutoGenerateColumns
to false
. If I set it to true
, it creates the rows, but I'm not allowed to set this property to true
(client wish).
What am I doing wrong? Here's the code:
public partial class Form1 : Form
{
private List<AStruct> _aCollectionList;
private BindingList<AStruct> _aCollectionBindingList;
public struct AStruct
{
public string ACode
{
get { return _aCode; }
set { _aCode = value; }
}
public string AName
{
get { return _aName; }
set { _aName = value; }
}
private string _aCode;
private string _aName;
}
public Form1()
{
InitializeComponent();
this.dataGridView1.AutoGenerateColumns = false;
}
private void button1_Click(object sender, EventArgs e)
{
_aCollectionList = new List<AStruct>();
FillCollectionStruct(true);
dataGridView1.DataSource = _aList;
dataGridView1.Refresh();
}
private void button2_Click(object sender, EventArgs e)
{
_aCollectionBindingList = new BindingList<AStruct>();
FillCollectionStruct(false);
dataGridView1.DataSource = _aBindingList;
dataGridView1.Refresh();
}
private void FillCollectionStruct(bool listBool)
{
AStruct aStruct = new AStruct();
for (int i = 0; i < 10; i++)
{
aStruct.ACode = i.ToString();
aStruct.AName = i.ToString();
if (listBool)
_aCollectionList.Add(aStruct);
else
_aCollectionBindingList.Add(aStruct);
}
}
}
In the Form1.Designer I do make the following columns:
//
// aCodeColumn
//
this.colorCodeColumn.DataPropertyName = "Code";
this.colorCodeColumn.HeaderText = "a code";
this.colorCodeColumn.Name = "aCodeColumn";
//
// aNameColumn
//
this.colorNameColumn.DataPropertyName = "Name";
this.colorNameColumn.HeaderText = "a name";
this.colorNameColumn.Name = "aNameColumn";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
难道你不能将绑定从: 更改
为:
这不应该起作用吗?
由于
DataPropertyName
是绑定到属性的名称,因此名称必须相同,否则编译器无法知道什么放在哪里。Can't you just change the bindings from:
To:
Shouldn't that work?
Since the
DataPropertyName
is what binds to your properties the names have to be the same, else there's no way for the compiler to know what goes where.如果我理解你的问题,你可以使用这样的东西。dataGridView1.Refresh();为了重新填充它,并在刷新行之前放置 this.dataGridView1.DataSource = _aCollectionBindingList;
If I understand your problem write you could use sth like this.dataGridView1.Refresh(); in order to repopulate it and before the line you refresh put this.dataGridView1.DataSource = _aCollectionBindingList;