Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datatable which is not binded to database.

How can i add this datatable as datasource of a DataGridView?

Please help me in this regard.

Thanks in Advance.
Posted
Updated 22-May-11 20:15pm
v3
Comments
Hemant__Sharma 23-May-11 1:17am    
What does that mean? elaborate your question properly what you want to achieve and what data you have right now.
--Hemant

try to do that in following way..

DataTable dt = null;
            DataRow dr = null;

            DataColumn idCoulumn = null;
            DataColumn nameCoulumn = null;
            DataColumn Description  = null;
            //int i = 0;

            dt = new DataTable();
            idCoulumn = new DataColumn("ID", Type.GetType("System.Int32"));
            nameCoulumn = new DataColumn("Name", Type.GetType("System.String"));
            Description = new DataColumn("Description",Type.GetType("System.String"));

            dt.Columns.Add(idCoulumn);
            dt.Columns.Add(nameCoulumn);
            dt.Columns.Add(Description);

            dr = dt.NewRow();
            dr["ID"] = 1;
            dr["Name"] = "Name1";
            dr["Description"]="Good";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["ID"] = 2;
            dr["Name"] = "Name2";
            dr["Description"] = "VeryGood";
            dt.Rows.Add(dr);
            
            GridView1.DataSource = dt;
            GridView1.DataBind();
 
Share this answer
 
try that way

DataGridView1.DataSource = new DataTable();
 
Share this answer
 
Try like this:

DataGridView1.DataSource = new DataTable();
 
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