C#:Bind Dictionary <string,list <string =“”>&gt;到DataTable

时间:2018-02-24 12:26:53

标签: c# winforms dictionary datatable

我有一个字典喜欢

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 有什么建议吗?

2 个答案:

答案 0 :(得分:2)

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

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;

结果是:

Re

答案 1 :(得分:0)

检查是否行或少于值而不是将值添加到新行 其他 向现有行添加值

        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++;
                    }

                }

            }
        }