DataGrid control doesn't display correctly in .aspx page
This article helps you resolve the problem that data grid may not appear when you view an .aspx page that contains a DataGrid control. This article refers to the Microsoft .NET Framework Class Library namespace System.Data.SqlClient
.
Original product version: ASP.NET
Original KB number: 313156
Symptoms
When you view an .aspx page that contains a DataGrid
control, the data grid may not appear. Alternately, only the header of the data grid may appear, but the data grid may not contain any data.
Cause 1: Data grid doesn't appear
The data grid does not appear at all if:
You do not set the
DataSource
property of theDataGrid
control. OrYou do not call the
DataBind
method of theDataGrid
control or theDataBind
method of thePage
object from your code.
Cause 2: Data grid header appears but data grid is empty
The data grid header appears, but the data grid does not contain any data if:
The data source does not contain any data. Or
You don't fill the dataset that is bound to the
DataGrid
with data at run time.Note
You must write code to explicitly fill the dataset so that the
DataGrid
is populated with data from the database at run time. You can do this when you use the designer to add the DataAdapter object and to generate a dataset at design time.
This problem occurs because the .aspx page retrieves the details of the header fields from the DataSetName.xsd file. The DataSetName.xsd file is created for each dataset when you generate a dataset at the design time. This file contains the Extensible Markup Language (XML) representation of the dataset. The .aspx page retrieves the header field details from this file. Therefore, the data grid header appears but the data grid is empty.
Resolution for the cause 1
Set the
DataSource
property of theDataGrid
to point to the appropriate data source. For example:DataGrid1.DataSource = dataSet11.Tables("Authors")
DataGrid1.DataSource = dataSet11.Tables["Authors"] ;
Note
If you programmatically retrieve the data from the data source, assign the data source to the
DataSource
property of theDataGrid
.If you use the designer to add a
DataAdapter
to the Web form and to generate a dataset, open theDataGrid
property page, and then select the appropriateDataSet
orDataView
object in theDataSource
property.Call the
DataBind
method of theDataGrid
control or theDataBind
method of the Page object. For example:'Use one of the following lines. 'Page.DataBind() DataGrid1.DataBind()
//Use one of the following lines. //Page.DataBind(); DataGrid1.DataBind();
Resolution for the cause 2
Make sure that your data source contains data and that your query returns data.
Use a
DataAdapter
to fill the dataset. For example:sqlDataAdapter1.Fill(dataSet11, "Authors")
sqlDataAdapter1.Fill(dataSet11,"Authors");
Status
This behavior is by design.
More information
Bind the DataGrid
Web server control to a data source through the DataSource
property of the DataGrid
to correctly render the data grid on the page. After you set the data source, you must also explicitly call the DataBind
method of the DataGrid
control or the DataBind
method of the Page
object from your code.
Steps to reproduce the behavior - data grid does not appear
Create a new ASP.NET Web Application project in Visual Basic .NET or Visual C# .NET.
Drag a
DataGrid
control from the toolbox, and drop the control onto the form.In the designer, double-click the .aspx page to open the code-behind window. Add the following code to the top of the code-behind window to add the required namespace references:
Imports System.Data Imports System.Data.SqlClient
using System.Data.SqlClient;
Replace the code in the
Form_Load
event with the following code:Note
You must change
uid = <username>
andpwd = <strong password>
to the correct values before you run this code. Be sure that UID has the appropriate permissions to perform this operation on the database.Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load 'Write code to access the database. Dim myConnection As New SqlConnection("server=(local);database=pubs;uid=<username>;pwd=<strong password>") Dim myDataAdapter As New SqlDataAdapter("select * from authors", myConnection) Dim myDataSet As New DataSet() myConnection.Open() myDataAdapter.Fill(myDataSet)'Uncomment the following line to resolve this problem. 'DataGrid1.DataSource = myDataSet DataGrid1.DataBind() End Sub
private void Page_Load(object sender, System.EventArgs e) { //Write code to access the database. SqlConnection myConnection = new SqlConnection("server=(local);database=pubs;uid=<username>;pwd=<strong password>"); SqlDataAdapter myDataAdapter = new SqlDataAdapter("select * from authors", myConnection); DataSet myDataSet = new DataSet(); myConnection.Open(); myDataAdapter.Fill(myDataSet); //Uncomment the following line to resolve this problem. //DataGrid1.DataSource = myDataSet; DataGrid1.DataBind(); }
Modify the connection string as appropriate for your environment.
Build the project, and then view the .aspx page in the browser. Notice that data grid does not appear.
Steps to reproduce the behavior - data grid header appears but data grid is empty
Create a new ASP.NET Web Application project in Visual Basic .NET or Visual C# .NET.
Drag a
DataGrid
control from the toolbox, and drop the control onto the form.Add a
SqlDataAdapter
control to the Web form. Complete the following steps in the DataAdapter Configuration Wizard to set the connection properties:Select New Connection to create a new connection to your SQL Server database. Select the server, type the logon information, and then select the Pubs database.
Under Query type, select SQL Statements, and then select Next.
Type the following SQL statement:
Select * From Authors
Select Finish.
Right-click the DataAdapter, and then select Generate dataset.
Set the
DataSource
property of theDataGrid
to the dataset that is generated.In the designer, double-click the .aspx page to open the code-behind window. Add the following code to the
Page_Load
event:DataGrid1.DataBind()
DataGrid1.DataBind();
Build the project, and then view the .aspx page in the browser.
Note
The header of the data grid appears, but the data grid does not contain any data.
References
For more information about the DataGrid
control and for samples that use this control, see DataGrid Class.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for