Click here to Skip to main content
15,914,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to know how to bind a table to a datagridview in a windows form and display it in form load...i cant use the smart tag, only the code behind.
thank for your help.
Posted

 
Share this answer
 
Comments
Dunstan89 24-Nov-11 7:02am    
i tried that link, its of no help plus it has some errors
I am not understanding your problem . It will good if u explain in detail

DataTable tab = new DataTable();
C#
tab.Columns.Add("ID", typeof(string));
               tab.Columns.Add("value", typeof(string));
               tab.Rows.Add("1", "No records found"

);
gridview1.datasource=tab ;


hope this will help u ...
 
Share this answer
 
try on page load like this
C#
try
       {

                     string      str = "select ReqId,FileNo,ReqControlNo,Requester,ReqDate,ReqTime,IssueDate,IssueTime,ReceivedDate,ReceiveTime from Issue where ReceivedDate between '" + txtReqDatefrom.Text + "' and '" + txtReqTo.Text + "' AND ReqControlNo='" + Session["ReqControlNo"].ToString() + "' order by ReceivedDate";

           }
           SqlDataAdapter da = new SqlDataAdapter(str, Db.GetConnection());
           DataTable table = new DataTable();
           table.Locale = System.Globalization.CultureInfo.InvariantCulture;
           da.Fill(table);
           GridView1.DataSource = table;
           GridView1.DataBind();
           GridView1.Visible = true;
       }
       catch (Exception ex)
       {
           Label1.Text = ex.ToString();
       }

note:replace Db.GetConnection() to your connection object
 
Share this answer
 
You need to use a DataAdapter to fill a DataSet and then use that to create a BindingSource. Then use the BindingSource as the DatagridView's DataSource. Lots of Sources involved, none of which include HP Sauce for the bacon, so here is a simple example:-

C#
string connectionString = "your Connection String";
            SqlConnection conn = new SqlConnection(connectionString);
            SqlDataAdapter da = new SqlDataAdapter("Select * From yourTable", conn);
            SqlCommandBuilder cmd = new SqlCommandBuilder(da);
            DataSet ds = new DataSet();
            da.Fill(ds, "yourTable");
            BindingSource bs = new BindingSource(ds, "yourTable");
            dataGridView1.DataSource = bs;


just put that in the form load event, obviously with your correct sql query and table name.

Hope that helps
 
Share this answer
 
ok if you are using datatable and has gridview control
than

datatable dt=new datatable();
gridview1.datasource=dt;
this will do for u if not plz tell.......
wat exactly u want
 
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