【问题标题】:Fixed DataGridView data binding修复 DataGridView 数据绑定
【发布时间】:2014-10-18 16:09:13
【问题描述】:

我正在编写一个计算我的扩展的应用程序。我有一个固定的 DatagridView,它有 12 列(1 月 - 12 月)和 5 行(类别)。 我的课:

Month -> contains the value of all 5 categories
Year -> SortedList<MonthSet.AcceptableMonths, Month>
AnnualSpend -> SortedList<ushort, Year>

程序在启动时会解析一个 XML 文件并将数据存储在相关的类中。 现在我有具有 5 个属性(每个类别一个)的“月份”类,并希望将特定月份绑定到相应的列。 我将能够遍历每个单元格并“手动”插入值,但我不知道这是否是一个好方法。此外,我能够在 ListBox 中绑定我所有的年份,但我不知道如何绑定数据(当我从 ListBox 中选择一年时)。我尝试以正常方式执行此操作,但我将丢失所有已定义的行,并且会添加新列。 我什至不知道这是否像我想的那样工作。如果没有,那么我可能必须遍历每个单元格。

我希望这是可以理解的,有人可以告诉我这是否可行。

谢谢 ;)

【问题讨论】:

    标签: c# .net datagridview


    【解决方案1】:

    代码假定AcceptableMonths 是一个Enum,定义如下,因此可以从整数转换。

    enum AcceptableMonths
    {
        JAN, FEB, MAR, ARP, MAY, JUN, JLY, AUG, SEP, OCT, NOV, DEC
    }
    

    您可以从AnnualSpend 构造一个DataSet,然后将其中一个表(年度费用)设置为DataGridViewDataSource。从您的类到 DataSet 的映射是。

    Month->A Column, Year->A DataTable, AnnualSpend->The entire DataSet
    

    构造DataSet的代码

    AnnualSpend annualSpend; //already populated 
    DataSet dataSet;
    
    private void PopulateDataSet()
    {
        dataSet = new DataSet();
    
        foreach (ushort yr in annualSpend.Keys)
        {
            Year year = annualSpend[yr];
            DataTable tableOfYear = new DataTable(yr.ToString()); //as key to access the Table
    
            //Adding columns
            List<string> columnNames = new List<string>();
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                columnNames.Add(column.Name);
                column.DataPropertyName = column.Name; //!!!Important!!!
                tableOfYear.Columns.Add(new DataColumn(column.Name));
            }           
    
            //Adding rows            
            DataRow newRowForFoodExpense = tableOfYear.NewRow();
            //then 4 data rows for other expense categories...
            //DataRow newRowForHouseRent = tableOfYear.NewRow();
    
            for (int m = 0; m < columnNames.Count; m++)
            {
                newRowForFoodExpense[columnNames[m]] = year[(AcceptableMonths)m].Food;   
                //then 4 data rows for other expense categories...              
            }
    
            tableOfYear.Rows.Add(newRowForFoodExpense);
            //then 4 data rows for other expense categories... 
    
            dataSet.Tables.Add(tableOfYear);
        }
    }
    

    然后从 ListBox 中为 DataGridView 设置 DataSource。

    listBox1.SelectedIndexChanged += (s, e) =>
    {
        ushort yr = (ushort)listBox1.SelectedItem;
        dataGridView1.DataSource = dataSet.Tables[yr.ToString()];
    };
    

    !!!重要!!!

    由于您的DataGridView在DataBinding之前已经定义了12列,所以您需要将DataGridView的每一列的DataPropertyName property设置为与DataTable的列名相同,否则绑定会添加新列。

    【讨论】:

    • 抱歉忘了说 AcceptableMonths 是一个枚举。我想我现在可以实现 INotifyPropertyChanged 但这没问题。尽管如此,一个非常好的解决方案。 :) 非常感谢。我需要很长时间才能弄清楚这一点。 ;)
    • 很高兴我能帮上忙。然而,困难的部分是,如果你将Month 的列表直接绑定到DataGridView,它们将被添加为 12 行,而不是列,所以首先你必须构造一个适合“reverted”的DataTable " Column 和 Row 定义...如果可以接受 Month 作为 Row 和 Category 作为 Column,您可以直接绑定到您的 Year 类(一个 BindingList)。
    • 是的,我已经尝试过了,它正在工作。必须做一些调整,但现在我有一个线索,知道从哪里开始。再次感谢;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    相关资源
    最近更新 更多