Monday, August 16, 2010

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

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?





this.sqlConnection1.Open();


this.dreader = this.sqlCommand3.ExecuteReader();


this.DataGrid1.DataSource = dreader;


this.DataGrid1.DataBind();


this.dreader.Close();


this.sqlConnection1.Close();How do I Bind Data to a Datagrid in a C# Windows Application?
I'm editing my response first, because actually you are correct--the DataBind() method doesn't exist for a Windows control. You would use the control.DataBindings.Add() method.





Assuming you have a DataGrid called ';MyGrid';, and you already have your sqlConnection1 SqlConnection object:





SqlDataAdapter objDA = null;


DataSet objDS = null;





objDA = new SqlDataAdapter(SQL, objConn.Conn);


objDS = new DataSet();





objDA.Fill(objDS);





MyGrid.DataSource = objDS.Tables[0];
  • love myspace
  • myspace girl
  • No comments:

    Post a Comment