By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 2,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

How do I Bind Data to a Datagrid in a C# Windows Application?

P: n/a
The below code works in a Web application, but does not work in a Windows
application. The DataBind() command only exists for Web forms. How do I write
this code for a Windows app? Can you give me the code on how to do that?

this.sqlConnection1.Open();
this.dreader = this.sqlCommand3.ExecuteReader...
this.DataGrid1.DataSource = dreader;
this.DataGrid1.DataBind();
this.dreader.Close();
this.sqlConnection1.Close();
Sep 29 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Steve,

You can load the data into a dataset and bind the dataset to a datagrid.

string connectionString = "something";

using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand com = new SqlCommand("SELECT * FROM CUSTOMERS", conn))
{
DataSet ds = new DataSet();

using (SqlDataAdapter da = new SqlDataAdapter(com))
{
da.Fill(ds);
dataGrid1.DataSource = ds.Tables[0];
}
}
}

Note, the using block will ensure close and dispose is called on the
objects.

On Fri, 29 Sep 2006 16:06:03 +0200, Steve B <Steve
<B@discussions.microsoft.com>wrote:
The below code works in a Web application, but does not work in a Windows
application. The DataBind() command only exists for Web forms. How do I
write
this code for a Windows app? Can you give me the code on how to do that?

this.sqlConnection1.Open();
this.dreader = this.sqlCommand3.ExecuteReader...
this.DataGrid1.DataSource = dreader;
this.DataGrid1.DataBind();
this.dreader.Close();
this.sqlConnection1.Close();



--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 29 '06 #2

P: n/a
Mel
a DataReader closes when your connection closes. You need to put the data
into a datatable or some object
and then attach that to the grid datasource.

"Steve B" <Steve B@discussions.microsoft.comwrote in message
news:72**********************************@microsof t.com...
The below code works in a Web application, but does not work in a Windows
application. The DataBind() command only exists for Web forms. How do I
write
this code for a Windows app? Can you give me the code on how to do that?

this.sqlConnection1.Open();
this.dreader = this.sqlCommand3.ExecuteReader...
this.DataGrid1.DataSource = dreader;
this.DataGrid1.DataBind();
this.dreader.Close();
this.sqlConnection1.Close();


Sep 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.