将列表列表绑定到dataGridView

时间:2017-11-07 23:19:21

标签: c# list data-binding datagridview

我已经定义了一个List(名为blockList),其类型列表contains 3 objects(称为rowList)包含2个Type Record对象。 在dataGridView中,我想一次显示一个3 blocks,即2 x 2 Array.

块的显示应由numericUpDown Control控制。

bs.CurrencyManager.Position中仅更改numUpDown_ValueChanged时,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; }
} 

2 个答案:

答案 0 :(得分:0)

public Form1()我评论了以下内容

以下是整个代码:

namespace listoflist
{
    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 Form1_Load(object sender, EventArgs e)
        {

        }

        private void numUpDown_ValueChanged(object sender, EventArgs e)
        {
            bs.CurrencyManager.Position = (int)numUpDown.Value;
            dg.CurrentCell = dg.Rows[Math.Min(dg.CurrentRow.Index + bs.CurrencyManager.Position, dg.Rows.Count - 1)].Cells[dg.CurrentCell.ColumnIndex];

        }
    }

    public class Record
    {
        public Record(int id, string customer)
        {
            ID = id;
            Customer = customer;
        }
        public Decimal? ID { get; set; }
        public string Customer { get; set; }
    } 
}

答案 1 :(得分:0)

现在我已经解决了我的问题,但我对我的解决方案并不满意 每次要显示新块时,NSString *serverOutput = @"Hi, I am <id>User</id>. I am 20 <id>years old</id>, and live in <id>some country</id>."; if([serverOutput containsString:@"</id>"]) { NSArray *arrSeparate = [serverOutput componentsSeparatedByString:@"</id>"]; NSString *output = @""; for(int i=0; i<arrSeparate.count; i++) { if([[arrSeparate objectAtIndex:i] containsString:@"<id>"]) { NSArray *arrTest = [[arrSeparate objectAtIndex:i] componentsSeparatedByString:@"<id>"]; if(output.length < 1) output = [arrTest objectAtIndex:1]; else output = [NSString stringWithFormat:@"%@\n%@",output,[arrTest objectAtIndex:1]]; } } serverOutput = output; } NSLog(@"%@", serverOutput); 中的旧块将被清除并替换为新块(请参阅bs)。因此displayNewBlock永远不会知道整个阻止列表,只知道该列表中的一个元素 在开始时,我确实希望找到一种方法,使bs可以使用完整的数据集,然后找到一种方法,使BindingSource的显示所需的部分数据可用。
这是我的代码的相关部分:

dataGridView