Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void grid_view()
        {
            string s = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
            string sel = "select * from TableName";
            SqlConnection con = new SqlConnection(s);
            SqlCommand cmd = new SqlCommand(sel, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            dataGridView1.DataSource = ds;
            dataGridView1.DataBind();
       }


i have this error when i run my code

"
'System.Windows.Forms.DataGridView' does not contain a definition for 'DataBind' and no extension method 'DataBind' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?)
"
C#

Posted
Comments
Wonde Tadesse 14-Aug-11 11:55am    
Your question tag and the exception is not matched. Are you asking Asp.NET or Window Forms GridView control ?

1 solution

When you set the datasource the grid should show the data. So just comment out the last line.

When setting the datasource, instead of using a dataset, use the datatable. For example:
dataGridView1.DataSource = ds.Tables[0];


If you're still not seeing anything in the grid, put a breakpoint in your code and check that there actually are rows in your datatable.
 
Share this answer
 
Comments
arslan3488 14-Aug-11 5:27am    
my grid view is not still displaying any row or any data
Wendelius 14-Aug-11 5:35am    
Did you debug through the code? Did the table have data? Also have you defined columns correctly or have you set AutoGenerateColumns to true?

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