How to Bind the DataSource in DataGridView

The "DataSource" property in an ASP.NET "DataGridView" control lets you bind data from a database to a Web grid on your pages. The DataGridView control makes it more convenient to display data, because you do not need to create columns and rows. Instead, the ASP.NET language creates the grid for you, and you just need to attach the data using the DataSource property.

Instructions

    • 1

      Click the Windows "Start" button, and type "visual studio." Press "Enter" to open Visual Studio, then after the software loads, open your Web project.

    • 2

      Connect to the database and query the information. In this example, a list of customers is retrieved. Use the following code template to set up your own connection and query:

      String connect = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=mydb;Data Source=server";

      da = new SqlDataAdapter("select * from customers, connect);

      SqlCommandBuilder builder = new SqlCommandBuilder(da);

    • 3

      Set up the data source. The "DataSource" property takes a table as input. The following code fills the table with the data you retrieved in step two:

      DataTable dt = new DataTable();

      da.Fill(dt);

    • 4

      Set the DataGridView's DataSource property. The following code shows you how to assign the table to the grid's property:

      gridview1.DataSource = dt;

Related Searches:

References

Comments

Related Ads

Featured