Microsoft KB Archive/316261

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

Article ID: 316261

Article Last Modified on 8/28/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft ASP.NET 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.



Steps to Reproduce the Behavior
 Create a new ASP.NET Web application in Microsoft Visual Basic .NET or Microsoft Visual C# .NET. Add a Web Form to your project.  Add the following code to the Web Form: <%@ import namespace=System.Data %> <%@ Page language=&quot;c#&quot; %>    void Page_Load(Object sender, EventArgs e) { //Create a DataTable. DataTable myTable  = new DataTable(&quot;myTable&quot;); // Create a DataColumn, and set various properties. DataColumn dc = new DataColumn; dc.DataType = System.Type.GetType(&quot;System.Decimal&quot;); dc.AllowDBNull = false; dc.Caption = &quot;Price&quot;; dc.ColumnName = &quot;Price_Test&quot;; 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[&quot;Price_Test&quot;] = 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; }     <asp:datagrid id=&quot;MyGrid&quot; runat=&quot;Server&quot; /> </HTML> </li> Save the Web Form.</li> 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.</li></ol>

To resolve this problem, replace the code in this sample <form method=&quot;post&quot; runat=&quot;server&quot;> <asp:datagrid id=&quot;MyGrid&quot; runat=&quot;Server&quot; /> with the following code: <form method=&quot;post&quot; runat=&quot;server&quot; ID=&quot;Form1&quot;> <asp:datagrid id=&quot;MyGrid&quot; AutoGenerateColumns = &quot;false&quot; runat=&quot;Server&quot;> <Columns> <asp:BoundColumn HeaderText=&quot;Price&quot; DataField=&quot;Price_Test&quot;></asp:BoundColumn> </Columns> </asp:DataGrid>

<div class="references_section">