且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中向数据表的列添加4个不同的列表

更新时间:2023-12-01 10:09:40

如果要将列表项添加到数据表中,请
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>