Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

PRB: DataGrid Does Not Automatically Generate UniqueIdentifier Fields with AutoGenerateColumns=True


View products that this article applies to.

Symptoms

When you use the DataGrid Web server control in your .aspx pages, corresponding columns are not generated automatically for fields of type UniqueIdentifier. This behavior occurs when the AutoGenerateColumns property for the DataGrid is set to True.

↑ Back to the top


Cause

When you set AutoGenerateColumns=True, DataGrid internally creates a collection of autogenerated columns. If the data type of a field is not one of Primitive, String, Decimal, or DateTime, DataGrid does not generate a column for that field. Because the UniqueIdentifier data type is actually a System.GUID type, DataGrid excludes this field while generating columns. For this reason, you do not see UniqueIdentifier column when the DataGrid is displayed on the .aspx page.

↑ Back to the top


Resolution

To work around this behavior, use one of the following methods. Both methods involve explicitly creating a bound column for fields of type UniqueIdentifier.

NOTE: You do not need to turn off the AutoGenerateColumns property. If you do turn it off, you must create every column explicitly. Otherwise, new columns that you create are added to the DataGrid in conjunction with autogenerated columns.

Method 1: Create a Column for UniqueIdentifier Field at Run Time and Add the Column to the Columns Collection

  1. Create a new BoundColumn, and then set the DataField and HeaderText properties.
  2. Add the column to the Columns collection of the DataGrid.
This workaround is more useful if you do not know which fields are returned from the DataSource. In this case, you can loop through the results from the DataSource and create a column for each field on the fly. Use the following code to achieve this:

Microsoft Visual Basic .NET
   Dim myBoundColumn As New BoundColumn()
   myBoundColumn.DataField = "UniqueIdentifierA"
   myBoundColumn.HeaderText = "UniqueIdentifierA"
   DataGrid1.Columns.Add(myBoundColumn)
				
Microsoft Visual C# .NET
   BoundColumn myBoundColumn = new BoundColumn();
   myBoundColumn.DataField = "UniqueIdentifierA";
   myBoundColumn.HeaderText = "UniqueIdentifierA";
   DataGrid2.Columns.Add(myBoundColumn); 
				

Method 2: Define a BoundColumn Template

If you know which fields are returned from the DataSource, you can create BoundColumn templates for the fields of type UniqueIdentifier. You must specify the DataField attribute as the field name that you want to display. The following sample code defines a DataGrid with a BoundColumn template for the UniqueIdentifier field:
   <asp:datagrid id=DataGrid1 runat="server" DataSource="<%# dataSet11 %>"
   Height="288px" Width="473px">
        <Columns>
	   <asp:BoundColumn datafield="fldUniqueIdentifier"
   HeaderText="UniqueIdentifier Column"></asp:BoundColumn>
        </Columns>
   </asp:datagrid>
				

↑ Back to the top


Status

This behavior is by design.

↑ Back to the top


More information

Currently, the DataGrid does not automatically generate columns for the following SQL data types. Also, you can use either of the preceding workarounds for these types:

  • Binary
  • Image
  • Sqlvariant
  • Timestamp
  • Uniqueidentifier
  • Varbinary

Steps to Reproduce the Behavior

NOTE: You must have a table that contains one or more fields of type UniqueIdentifier or any of the SQL data types mentioned in the "More Information" section of this article. Make sure that you have some data in that table.
  1. Create a new ASP.NET Web application by using Microsoft Visual Basic .NET or Microsoft Visual C# .NET.
  2. Drag a DataGrid control from the Toolbox onto the Web form.
  3. Drag a SQLDataAdapter control onto the Web form. Follow these steps in the DataAdapter Configuration Wizard to set the connection properties:
    1. Click New Connection to create a new connection to your SQL database. Select the server, the logon information, and the database that you want.
    2. Under Choose a Query type, select SQL Statements, and then click Next. Type the following SQL statement. Replace mytable with the name of the table that contains a UniqueIdentifier column:
      Select * From mytable
    3. Click Finish.
  4. Right-click DataAdapter, and then click the Generate Dataset command.
  5. Set the DataSource property of the DataGrid to the generated DataSet.
  6. In the Designer, double-click the .aspx page to go to the Code-behind window. In the page_Load event, paste the following code. Replace mytable with your table name.

    Microsoft Visual Basic
       sqlDataAdapter1.Fill(dataSet11, "mytable") 
       DataGrid1.DataBind()
    					
    Microsoft C#
       sqlDataAdapter1.Fill(dataSet11,"mytable");  
       DataGrid1.DataBind();
    					
  7. Build the project, and then view the .aspx page in the browser. Notice that DataGrid does not display a column for the UniqueIdentifier field.

↑ Back to the top


Keywords: KB313155, kbservercontrols, kbprb, kbdatabinding, kbdatabase

↑ Back to the top

Article Info
Article ID : 313155
Revision : 3
Created on : 7/25/2012
Published on : 7/25/2012
Exists online : False
Views : 602