【发布时间】:2018-04-20 12:13:30
【问题描述】:
我定义了一个列表(称为blockList),其中contains 3 objects 的类型列表(称为rowList)包含2 个记录类型的对象。
在 dataGridView 中,我想一次显示 3 blocks 之一,即 2 x 2 Array.
块的显示应该由一个numericUpDown控件来控制。
当numUpDown_ValueChanged 中仅更改bs.CurrencyManager.Position 时,dataGridView 将不会像我预期的那样更新。仅当此方法中的第二行未注释时才执行此操作。
另一个问题是dataGridview 中的最后一行allows adding of new lines disappears。设置allowNew-Property 没有帮助。
我确实有一种朦胧的感觉,即我实现数据绑定的方法不正确。
我的代码:
public partial class Form1 : Form
{
public List<Record> rowList;
public List<List<Record>> blockList;
public BindingSource bs = new BindingSource();
public Form1()
{
InitializeComponent();
int rowListLength = 2;
int blockListLength = 3;
blockList = new List<List<Record>>(blockListLength);
for (int i = 0; i < blockListLength; i++)
{
blockList.Add(new List<Record>());
blockList[i] = new List<Record>(rowListLength);
for (int j = 0; j < rowListLength; j++)
blockList[i].Add(new Record(10 * i + j, "name_" + (10 * i + j).ToString()));
}
dg.AutoGenerateColumns = false;
dg.Columns["ID"].ReadOnly = false;
dg.Columns["ID"].DataPropertyName = "ID";
dg.Columns["Customer"].DataPropertyName = "Customer";
bs.DataSource = typeof(List<List<Record>>);
foreach (List<Record> block in blockList)
bs.Add(block);
bs.CurrencyManager.Position = 0;
dg.DataSource = (List<Record>)bs.CurrencyManager.Current;
}
private void numUpDown_ValueChanged(object sender, EventArgs e)
{
bs.CurrencyManager.Position = (int)numUpDown.Value;
//dg.DataSource = (List<Record>)bs.CurrencyManager.Current;
}
}
public class Record
{
public Record(int id, string customer)
{
ID = id;
Customer = customer;
}
public Decimal? ID { get; set; }
public string Customer { get; set; }
}
【问题讨论】:
标签: c# list data-binding datagridview