【问题标题】:Binding a List of Lists to a dataGridView将列表列表绑定到 dataGridView
【发布时间】: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


    【解决方案1】:

    在您的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; }
        } 
    }
    

    【讨论】:

    • Tnx 为您服务。但很抱歉,它似乎不适合我。
    • :Tnx 为您提供帮助。但我很抱歉,它似乎对我不起作用。 dg 中仅显示我的三个块中的第一个。仅当再次重新激活 numUpDown_ValueChanged 中的原始注释行时,才会显示其他两个块。用于创建新行的附加行仍然不可见。所以行为与以前基本相同。抱歉,我之前的评论是无意中发布的。
    【解决方案2】:

    现在我已经解决了我的问题,但我对我的解决方案并不完全满意。
    每次显示新块时,bs 中的旧块被清除并被新块替换(参见displayNewBlock)。所以bs 从不知道整个阻止列表,而只知道该列表的一个元素。
    一开始我确实希望我能找到一种方法让BindingSource 可以使用完整的数据集,然后找到一种方法让dataGridView 可以使用显示所需的那部分数据。
    这是我的代码的相关部分:

        public Form1()
        {
            // Initialisations left out
            dg.AutoGenerateColumns = false;
            dg.AllowUserToAddRows = true;
            dg.Columns["ID"].DataPropertyName = "ID";
            dg.Columns["Customer"].DataPropertyName = "Customer";
    
            bs.DataSource = typeof(List<Record>);
            bs.AllowNew = true;
            dg.DataSource = bs;
    
            displayNewBlock(0);
        }
    
        private void numUpDown_ValueChanged(object sender, EventArgs e)
        {
            displayNewBlock((int)numUpDown.Value);
        }
    
        private void displayNewBlock(int blockIndex)
        {
            bs.Clear();
            foreach (Record row in blockList[(int)numUpDown.Value])
                bs.Add(row);
        } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 2014-08-18
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多