Wednesday, April 8, 2009

ADO.NET IN 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 and System .data.sqlclient namespaces.

public void Form1_Load(object sender, EventArgs e)
  {
SqlConnection  myconnection  = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True");
DataSet mydataset  = new DataSet();

SqlDataAdapter myadapter = new
SqlDataAdapter("select *from products",myconnection);
myadapter.Fill(mydataset, "products");
Datagridview1.DataSource = ds.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.                                                        

1 comment:

  1. Correction :
    ds.tables["products"] = mydataset.tables["products"];

    ReplyDelete