PRB: DataColumn.Caption Property Does Not Set Column Headers of DataGrid Web Server Control as Expected (316261)



The information in this article applies to:

  • Microsoft ASP.NET (included with the .NET Framework) 1.0
  • Microsoft ASP.NET (included with the .NET Framework 1.1)

This article was previously published under Q316261

SYMPTOMS

If you use the DataColumn.Caption property to set the display text for column headers of a DataGrid Web server control, the column headers may not be set to the text that you specify.

CAUSE

Columns are abstracted as properties of a DataRow object. By design, the properties are named according to the value of the ColumnName property, not the value of the Caption property so that you can bind to arbitrary objects and not just to DataRow objects.

RESOLUTION

To set the header text for columns so that the text differs from the value of the ColumnName, set the AutoGenerateColumns property to false. Additionally, you must explicitly declare the column and specify the header text for the column.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new ASP.NET Web application in Microsoft Visual Basic .NET or Microsoft Visual C# .NET.
  2. Add a Web Form to your project.
  3. Add the following code to the Web Form:
    <%@ import namespace=System.Data %>
    <%@ Page language="c#" %>
    <HTML>
    	<HEAD>
    	<script runat="server">	
    		void Page_Load(Object sender, EventArgs e) {
    		//Create a DataTable. 
    		DataTable myTable	= new DataTable("myTable");
    		// Create a DataColumn, and set various properties.
    		DataColumn dc = new DataColumn();	
    		dc.DataType = System.Type.GetType("System.Decimal");
    		dc.AllowDBNull = false;
    		dc.Caption = "Price";
    		dc.ColumnName = "Price_Test";
    		dc.DefaultValue = 25;
    		// Add the column to the table.
    		myTable.Columns.Add(dc);
    		// Add ten rows, and then set their values.
    		DataRow myRow;
    		for(int i	= 0; i < 10; i++)
    		{
    			myRow =	myTable.NewRow(); 
    			myRow["Price_Test"] = i + 1;
    			//Be sure to add the new row to the DataRowCollection.
    			myTable.Rows.Add(myRow);
    		}	
    
    		DataSet myDataSet = new DataSet();
    		//Add the new DataTable to the DataSet.
    		myDataSet.Tables.Add(myTable);
    		MyGrid.DataSource = myDataSet.Tables[0].DefaultView;
    		MyGrid.DataBind();
    }
    		</script>
    	</HEAD>
    	<body>
    		<form method="post" runat="server">
    			<asp:datagrid id="MyGrid" runat="Server" />
    		</form>
    	</body>
    </HTML>
    					
  4. Save the Web Form.
  5. View the page in the browser. Notice that the HeaderText property is set to Price_Test (the value of the ColumnName) instead of the caption (Price) that you specified.
To resolve this problem, replace the code in this sample
<form method="post" runat="server">
    <asp:datagrid id="MyGrid" runat="Server" />
</form>
				
with the following code:
<form method="post" runat="server" ID="Form1">
   <asp:datagrid id="MyGrid" AutoGenerateColumns = "false" runat="Server">
   <Columns>
    <asp:BoundColumn HeaderText="Price" DataField="Price_Test"></asp:BoundColumn>
   </Columns>
   </asp:DataGrid>
 </form>
				

REFERENCES

For additional information about the ASP.NET roadmap, click the article number below to view the article in the Microsoft Knowledge Base:

305140 INFO: ASP.NET Roadmap

For more information about data binding and how to work with the DataGrid Web server control, see the following ASP.NET QuickStart tutorial: For general information about ASP.NET, see the following MSDN newsgroup:

microsoft.public.dotnet.framework.aspnet
microsoft.public.dotnet.framework.aspnet


Modification Type:MajorLast Reviewed:9/20/2006
Keywords:kbDataBinding kbprb kbServerControls kbWebForms KB316261