【问题标题】:C#: Bind Dictionary<string, List<string>> to DataTableC#:将 Dictionary<string, List<string>> 绑定到 DataTable
【发布时间】:2018-08-04 09:29:44
【问题描述】:

我有一本字典像

Dictionary<string, List<string>> first=new Dictionary<string, List<string>>();

我想将此字典绑定到数据表,以便数据表 ColumnName 应该是字典的键,并且相应的列应该包含它们的字典值。 我尝试了什么:

Dictionary<string, List<string>> some= new Dictionary<string, List<string>>();
 System.Data.DataTable dt = new System.Data.DataTable();
            foreach (var entry in some)
            {
                if (entry.Value.Count > 0)
                {
                    dt.Columns.Add(entry.Key); 
                    //entry.Value.count is not same for all entry.Key                 
                    foreach (var value in entry.Value)
                    {
                        DataRow row = dt.NewRow();
                        row[entry.Key] = value;
                        dt.Rows.Add(row);
                    }
                }              
            }

我当然知道,上面的代码有一些错误来实现以下结果 DesirrdResultImage 有什么建议吗?

【问题讨论】:

  • 有什么建议吗?
  • 这应该是row[entry.Key] = value;
  • @FaizanRabbani 是的,我现在要编辑。
  • 确切的错误是什么?
  • 对于每一列,数据都添加到前一列最后一行的旁边。 @Haytam

标签: c# winforms dictionary datatable


【解决方案1】:

这是一个可能的解决方案(请注意,我认为这不是最好的方法,但我希望它能帮助指导您):

Dictionary<string, List<string>> some = new Dictionary<string, List<string>>
{
    { "Key1", new List<string>
        {
            "Val1_1",
            "Val2_1",
            "Val3_1"
        }
    },
    { "Key2", new List<string>
        {
            "Val1_2",
            "Val2_2",
            "Val3_2"
        }
    }
};

DataTable dt = new DataTable();
var keys = some.Keys;

// Add all the columns from the beginning
dt.Columns.AddRange(keys.Select(key => new DataColumn(key)).ToArray());
// Get the rows number using the Max count of the lists (assuming the length of the lists might change, otherwise just use some.Values[0].Count)
int rowsNumber = some.Values.Max(s => s.Count);

for (int i = 0; i < rowsNumber; i++)
{
    var row = dt.NewRow();

    // Set all the values depending on the keys
    foreach (var key in keys)
    {
        if (some[key].count <= i)
            break;

        row[key] = some[key][i];
    }

    dt.Rows.Add(row);
}

dataGridView1.DataSource = dt;

结果是:

【讨论】:

  • 'row[key] = some[key][i] ' 如果所有字典的计数不同,则索引超出范围异常。请编辑。
  • 在 row[key]=some[key][i] 之前添加 if (some[key].count
【解决方案2】:

检查是否没有行或小于值比将值添加到新行 别的 为现有行增加价值

        DataTable dt = new DataTable();
        int i = 0;
        foreach (var entry in some)
        {
            if (entry.Value.Count > 0)
            {
                dt.Columns.Add(entry.Key);
                DataRow row;
                //entry.Value.count is not same for all entry.Key                 
                foreach (var value in entry.Value)
                {

                    int ValueCount = entry.Value.Count();
                    if (dt.Rows.Count <= ValueCount)
                    {
                        row = dt.NewRow();
                        row[entry.Key] = value;
                        dt.Rows.Add(row);
                    }
                    else
                    {
                        row = dt.Rows[i];
                        row[entry.Key] = value;
                        i++;
                    }

                }

            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多