C#:绑定字典< string,List< string>>到数据表 [英] C#: Bind Dictionary<string, List<string>> to DataTable

查看:55
本文介绍了C#:绑定字典< string,List< string>>到数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本字典,就像

  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 =新的System.Data.DataTable();foreach(某些变量输入){如果(entry.Value.Count> 0){dt.Columns.Add(entry.Key);//entry.Value.count对于所有entry.Key都不相同foreach(entry.Value中的var值){DataRow行= dt.NewRow();row [entry.Key] =值;dt.Rows.Add(row);}}} 

我当然知道,上面的代码存在一些错误,无法实现以下结果

I have one dictionary Like

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

I want to bind this dictionary to data table such that data table ColumnName should be key of the dictionary and respective columns should contain their dictionary values. What I tried:

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

Surely I know, above code is having some errors to achieve following result DesirrdResultImage any suggestions?

解决方案

Here's a possible solution (note that I don't think this is the best way to do this, but I hope it'll help guide you):

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;

The result is:

这篇关于C#:绑定字典&lt; string,List&lt; string&gt;&gt;到数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆