Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
harshada3 11-Jun-12 5:33am    
u can't add list as datacolumn but u can add all the list in to array list and then bind it

1 solution

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
C#
	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>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900