Click here to Skip to main content
15,906,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a datagridview in c#.net3.0 and I have two string values stored in variables.
Now I want to bind these two string values to columns of a datagridview.

Regards,
Nagaraju.n
Posted
Updated 15-Nov-10 19:52pm
v2

1 solution

For binding the datagridview need a datatable or dataset.
You can add the 2 variable values into a datatable and then bind the same with datagridview

Creating a datatable with two fileds the id and value
DataTable aTable = new DataTable("TableName");
               aTable.Columns.Add("ID", typeof(int));
               aTable.Columns.Add("VarValue", typeof(string));
               DataRow dr;
               dr = aTable.NewRow();
               dr["ID"] = 1;
               dr["VarValue"] = "Variable1 Value";
               aTable.Rows.Add(dr);

               dr = aTable.NewRow();
               dr["ID"] = 2;
               dr["VarValue"] = "Variable2 Value";
               aTable.Rows.Add(dr);


Now you need to bind the data table with the gird-view
GridView1.DataSource = aTable;
     GridView1.DataBind();


Hope this will help you :)
 
Share this answer
 

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