Microsoft Knowledge Base Email Alertz

(316261) - 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.

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 316261 - Last Review: August 28, 2007 - Revision: 3.5

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

This article was previously published under Q316261

On This Page

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  (http://kbalertz.com/Feedback.aspx?kbNumber=305140/EN-US/ ) INFO: ASP.NET Roadmap
For general information about ASP.NET, see the following MSDN newsgroup:
microsoft.public.dotnet.framework.aspnet
microsoft.public.dotnet.framework.aspnet (http://msdn.microsoft.com/newsgroups/default.aspx?query=ASP.NET&dg=&cat=en-us-msdn&lang=en&cr=US&pt=&catlist=774F24A2-F71F-425F-AC2B-DC48AB0DA5C9&dglist=&ptlist=&exp=&sloc=en-us)

APPLIES TO
  • Microsoft ASP.NET 1.0
  • Microsoft ASP.NET 1.1
Keywords: 
kbdatabinding kbprb kbservercontrols kbwebforms KB316261
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate