Thursday, January 20, 2011

ADO.NET Using C# - Part -2

           HOW TO DISPLAY  DATA IN A DATA GRID
In the previous post i have discussed about the sqlclient classes,now i am going to make a project which will retrieve the datatable (products)  from the database(northwind).
Click FILE-> NEW PROJECT Drag datagridview on form from toolbox.Double Click the form so  that you can able to write code in FORMLOAD event.Dont forget to add System.data andSystem .data.sqlclient namespaces.
public void Form1_Load(object sender, EventArgs e)
  {
SqlConnection  myconnection  = new SqlConnection("Data Source=.;Initial Catalog=Northwind;IntegratedSecurity=True");
DataSet mydataset  = new DataSet();

SqlDataAdapter myadapter = new
 SqlDataAdapter("select *from products",myconnection);
myadapter.Fill(mydataset, "products");
Datagridview1.DataSource =  mydataset.Tables["products"];
}

After debugging  you will see that all the columns of products  will display in a datagrid with its respective data.After execute it Try to display customers data.
Note: Here name of data grid is Datagridview1.