Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dataGrid view in my windows form and now i want bind it programatically. i write this code:
C#
string ConnString = "";
sqlConnection conn = new SqlConnection (ConnString);
string sql = "";
sqlDataAdapter da = new SqlDataAdapter(sql,conn);
conn.open()l
dataset ds = new dataset();
da.fill(ds);
conn.close();
datagridview1.DataSource = ds;

My code is right and ds hase 1 record and fill right but my grid dont show any record. do you know why? Am I missing anything?
Posted
Updated 23-Jul-19 23:29pm
v2

Yes, you are missing one line of code. You forgot to bind the data to the grid. You just set the data for grid. You need to trigger it's bind method.

Put this at the end:
C#
datagridview1.Databind();

This should do.

UPDATE:
For Winforms: Datagrid sample[^]
 
Share this answer
 
v3
after:
datagrdview1.DataSource = ds;

add:
datagridview1.DataBind();
 
Share this answer
 
but its windows form and has no databind()

what is insted of databind()?
 
Share this answer
 
Comments
R. Giskard Reventlov 25-Jul-10 3:27am    
Do not respond to answers with an answer: use the comments!
ptvce 25-Jul-10 3:40am    
thanks, now say what should i write insted of DataBind()?
Sandeep Mewara 25-Jul-10 4:23am    
Reason for my vote of 1
Not an answer.
Sandeep Mewara 25-Jul-10 4:28am    
Follow the steps mentioned in this article: http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx

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