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

I am new in windows application. I am trying to bind gridview but am unable to bind it. When using break and view dataset data set contains result but not showing in grid.

code is

C#
string sql = @"select * from SmsContent";
      SqlConnection conn = new SqlConnection(ConnectionString);
      SqlDataAdapter da = new SqlDataAdapter(sql, conn);
      DataSet ds = new DataSet();
      da.Fill(ds, "SmsContent");

      DG_sms.DataSource = ds;
Posted
Updated 17-Jan-11 3:48am
v3

It could be that your DataGridView (it is a DGV, isn't it) is confused about which table from the DataSet to use(I know that there is only 1, but still).

Try
C#
string sql = @"select * from SmsContent";
      SqlConnection conn = new SqlConnection(ConnectionString);
      SqlDataAdapter da = new SqlDataAdapter(sql, conn);
      DataSet ds = new DataSet();
      da.Fill(ds, "SmsContent");

      DG_sms.DataSource = ds.Tables["SmsContent"];


instead.

Alternatively keep your existing code and set the DataMember property of your grid
C#
DG_sms.DataMember = "SmsContent";
 
Share this answer
 
v2
Comments
Sandeep Mewara 17-Jan-11 10:01am    
You beat me to it! :)
Dalek Dave 17-Jan-11 10:11am    
Good Call, Henry.
Sandeep Mewara 17-Jan-11 10:23am    
:) Ok.. here is my upvote...
thus +15 extra ;) (only +25 for correct answer but +40 if I upvote ;) )
I guess you missed using datatable of dataset.

Try:
DG_sms.DataSource = ds.Tables[0];
 
Share this answer
 
Comments
prateekfgiet 17-Jan-11 10:10am    
thanks its working but i put DG_sms.DataSource = ds.Tables["SmsContent"]; in the place of DG_sms.DataSource = ds.Tables[0];
Henry Minute 17-Jan-11 10:13am    
Hey! No fair!

prateekfgiet used my code but marked you as the answer. :))
Sandeep Mewara 17-Jan-11 10:24am    
Compensated with interest :)
Sandeep Mewara 17-Jan-11 10:27am    
BTW, looks like my answer was simpler to use/understand for OP :P
Henry Minute 17-Jan-11 11:01am    
Reciprocated.
OK I just tested this and it is working:

SqlConnection conn = new SqlConnection("YOUR CONNECTION STRING");
     conn.Open();
     SqlCommand cmd = new SqlCommand(@"YOUR SQL QUERY", conn);
     SqlDataAdapter da = new SqlDataAdapter(cmd);
     DataSet ds = new DataSet();
     da.Fill(ds);
     dataGridView1.DataSource = ds.Tables[0].DefaultView;
     dataGridView1.AutoGenerateColumns = true;
     conn.Close();
 
Share this answer
 
Comments
Henry Minute 17-Jan-11 20:34pm    
Two people have already given essentially the same answer, several hours ago. If you believe that your solution adds something then you should explain what it adds in your answer.
Mastersev 17-Jan-11 21:01pm    
because of the autogenerate clumns

dataGridView1.AutoGenerateColumns = true;
You just missed last line at the end.

C#
DG_sms.DataSource = ds;
DG_sms.DataBind();
 
Share this answer
 
Comments
Henry Minute 17-Jan-11 9:54am    
I am not sure that this is the answer. I haven't done any tests to confirm that though :).

I have NEVER had to use the DataBind() method.
Sandeep Mewara 17-Jan-11 10:00am    
Comment from OP:
i have tried this but when i m using that missing line it gives error

"System.Windows.Forms.DataGridView' does not contain a definition for 'DataBind'............."
Sandeep Mewara 17-Jan-11 10:01am    
Hiren, it's winforms and not web app.
In winforms, you don't need to do databind().
Hiren solanki 19-Jan-11 3:57am    
sorry, I was not aware of the UI as OP hasn't mentioned it.
Manfred Rudolf Bihy 19-Jan-11 16:13pm    
Can't really help you on that one! +-0

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