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";
Can’t you just change the bindings from:
To:
Shouldn’t that work?
Since the
DataPropertyNameis 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.