将列表数据添加到DataTable中 [英] Adding list data into DataTable

查看:121
本文介绍了将列表数据添加到DataTable中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我使用下面的代码在DataTable中添加列表数据

我想转换字符串列表数据表。



Hi All,

I am using the below code to add list data in DataTable
I want to convert list of string in to Datatable.

  //code to convert list to datatable
        public static System.Data.DataTable TODataTable<t>(List<string> list)
        {

            System.Data.DataTable table = new System.Data.DataTable();
            
            //Add columns only 4 columns are required
                for (int i = 0; i < 4; i++)
                {
                    table.Columns.Add();
                }
                

                //add rows
                foreach(var array in list)
                {
                    table.Rows.Add(array);
                }
                
            return table;
            }







但是在结束表中它是空的,它不包含任何数据。 />


我的问题是我的代码出错了怎么办?

推荐答案

适合我!

Works for me!
List<string> list = new List<string>() { "hello", "there" };
DataTable dt = TODataTable<string>(list);
dataGridView1.DataSource = dt;

我看到四列,两行:你好和那里。



我从头开始使用调试器来查看传递方法的确切内容。

And I see four columns, and two rows: "Hello" and "there".

I'd start by using the debugger to look at exactly what you are passing the method.


这篇关于将列表数据添加到DataTable中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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