Datagrids and SQL DataTypes
1. 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.
2. 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>
NOTE: After you Bound the column using this method, you can go to the command Builder and uncheck the "visible" box
YOU CAN USE THE METHODS ABOVE WITH THE FOLLOWING DATA TYPES
• Binary
• Image
• Sqlvariant
• Timestamp
• Uniqueidentifier
• Varbinary