如何在C#中向数据表的列添加4个不同的列表 [英] how to add 4 different list to column of the datatable in c#
本文介绍了如何在C#中向数据表的列添加4个不同的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有四个不同的列表.我想将每个列表绑定到每个datagridview列.
为此,我正在通过创建一个数据表进行尝试.
并将每个列表添加为datatable列.最后,我将该数据表绑定到datagridview.
但是我坚持将列表添加到datacolumn对象..
我做对了吗?
还是任何人都可以为此提供一些代码逻辑??
问候,
Mohana.
Hi all,
I have four different lists. I want to bind each list to each datagridview column.
For that, i am trying it by creating one Datatable.
And add each list as the datatable column.Finally i ll bind that datatable to datagridview.
But i stuck up in adding the lists to the datacolumn object..
Am i doin in right way?
Or can anyone give some code logic to that??
Regards,
Mohana.
推荐答案
如果要将列表项添加到数据表中,请
dt.Columns.Add(name [0] .ToString());//此处的名称是字符串类型列表
尝试这样
if you want to add list item to a datatable
dt.Columns.Add(name[0].ToString ());//here name is string type list
try like this
public DataTable GetResultsTable()
{
// Create the output table.
DataTable dt = new DataTable();
// Loop through all process names.
for (int i = 0; i < this.items.Count; i++)
{
// The current process name.
string name = this.heading[i];
// Add the program name to our columns.
dt.Columns.Add(name);
// Add all of the memory numbers to an object list.
List<object> objectNumbers = new List<object>();
// Put every column's numbers in this List.
foreach (double number in this.items[i])
{
objectNumbers.Add((object)number);
}
// Keep adding rows until we have enough.
while (dt.Rows.Count < objectNumbers.Count)
{
dt.Rows.Add();
}
// Add each item to the cells in the column.
for (int a = 0; a < objectNumbers.Count; a++)
{
dt.Rows[a][i] = objectNumbers[a];
}
}
return dt;
}
//use this method to bind gridview
GridView1 .DataSource = GetResultsTable();
GridView1.DataBind();
//declare list
List<string> heading = new List<string>();
List<double[]> items = new List<double[]>();
//add items to list
heading.Add("Cat");
items.Add(new double[]
{
1.0,
2.2,
3.4
});
heading.Add("Dog");
items.Add(new double[]
{
3.3,
5.0,
7.0
});
</string></string>
这篇关于如何在C#中向数据表的列添加4个不同的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文