Steps to Reproduce the Behavior
You can view a
DataGrid control in a Web Form designer window at design time. The
DataGrid displays default columns unless you set the properties of the
DataGrid properties to connect to a data source. This design-time behavior differs from the run-time behavior. At run time, the code-behind is run, and the
DataGrid shows the results of the code execution.
The following steps use Microsoft Visual Basic .NET and the Microsoft SQL Server
pubs sample database. These steps also apply to other languages that use the
DataGrid control in an ASP.NET application.
- Create a new ASP.NET Web Application project in Visual Basic .NET. Web Form1 is created by default.
- Drag a DataGrid control from the Web Form section of the toolbox onto WebForm1.
NOTE: The columns are named "Column0," "Column1," and "Column2" by default. - Drag a SqlDataAdapter control from the Data section of the toolbox onto WebForm1. A series of dialog boxes prompt you to configure the DataAdapter settings.
- Initially, you are prompted to select a connection. Verify that the drop-down list includes a connection for the pubs database. If no connection for the pubs database exists, click New Connection, and then click Next.
- In the Choose a Query Type dialog box, click Use SQL Statements, and then click Next.
- In the Generate SQL Statements dialog box, type a SQL statement, such as select * from authors. You can also click Advanced Options, and then clear the Generate Insert, Update and Delete Statements check box because this demonstration does not use these functions.
- Click Finish. A DataAdapter button and a Connection button appear at the bottom of the WebForm1 designer window. By default, the DataAdapter control is named SqlDataAdapter1, and the Connection is named SqlConnection1.
- Right-click DataAdapter, and then click Generate Dataset. In the dialog box, click OK. By default, the DataSet object is named DataSet11.
- In the WebForm1 designer window, click DataGrid1. Press F4 to view the properties of DataGrid1.
- In the DataSource property, click DataSet11 from the list.NOTE: The DataGrid displays all columns from the Authors table. Column headers are filled in with the names of the columns.
- Clear the DataSource property.NOTE: The DataGrid displays the default columns, which are labeled "Column0," "Column1," and "Column2."
- Open the code-behind window for WebForm1. In the Solution Explorer window, right-click WebForm1, and then click View Code.
- Copy the following code, and paste the code into the Page_Load event:
Dim ds As New DataSet()
SqlDataAdapter1.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
To find the Page_Load event, click WebForm1 in the Class Name list, and then click Page_Load in the Method Name list.
To edit the code, compare the names of the grid and the adapter on your page to the names in the code. Change the names in the code as necessary. - Return to the Designer view for WebForm1.
- Verify that the DataSource property of the DataGrid control is cleared.NOTE: The DataGrid displays the default columns, which are labeled "Column0," "Column1," and "Column2."
The preceding steps demonstrate design-time behavior. You must set the
DataSource property of the
DataGrid control by setting the properties or by modifying the code. You can then see the data from the table by viewing the page in a browser.