|
Use AutoGenerateColumns Attribute To Control DataGrid Column Rendering
In previous
articles we have limited the discussion to very elementary use of DataGrid
control. This was just to get started and have some kind of foundation to build
on. Now we will start delving into some intermediate level of use. In this
artcile we will focus on one question.
Can I select the columns, from the datasource, which should be renderd?
The datasource for your data grid may have quite a few data columns. But you
may not want to display the information from all of them. And in some cases,
you may want to combine the information from multiple fields to display in one
data column of the grid. And extend our example we have added one more field to
the dataset. This field contains the URLs for the artcile titles in the
dataset. Now when we want to display the list of articles, there is no point
displaying the long URLs to the client. It would be nice if we could present
the data in such a form that the client only sees the titles and click on them
to access the URLs. That means we need a way so that Hyperlink data
field from the datasource gets combined with Title field in one DataColumn.
DataGrid control exposes AutoGenerateColumns attribute.
By default this attribute value is set to true. What this means is
that when DataGrid gets databound to the source, it will render
all the data fields that it can. Notice that we have used the term render fields
that it can. This is how it works.
When you bind a data source to DataGrid control, it generates BoundColumn
objects for each data column. As the name implies, a BoundColumn is
a column in the grid and is bound to a data field in the datasource. It is used
to render information in text format. That means if a field contains data that
is not of Primitive types, String, DataTime
and Decimal type, it will exclude those columns from rendering.
This does not mean that you can't have any other data types in the source. But
to render those data types, you will need to use TemplateColumn.
We will discuss that in our next article.
There are 5 types of colums that you can add to DataGrid control.
-
EditCommandColumn
-
HyperLinkColumn
-
BoundColumn
-
ButtonColumn
-
TemplateColumn
The names of the columns are very descriptive. If you want to render a field in
a form that does not fit in first four types, then you can use TemplateColumn
for that.
Back to our orginal question. How do we control what columns to display? Here
are the steps that you need to follow to accomlish this. First we will show
this can be done using asp design time tags. And then we will show
how to do it programtically.
Now we will show how this can be accomplished programtically in code behind
file. Now you don't have to supply any Columns or BoundColumn
tags in aspx file. All the steps described earlier will be done
prgramtically now. We have created InitializeBoundColumns method
in our class that adds the columns to Columns collection of DataControl
control.
private void InitializeBoundColumns()
{
DefaultGrid.AutoGenerateColumns = false;
BoundColumn indexCol = new BoundColumn();
indexCol.DataField = "Index";
indexCol.HeaderText = "#";
BoundColumn descCol = new BoundColumn();
descCol.DataField = "Description";
descCol.HeaderText = "Description";
HyperLinkColumn urlCol = new HyperLinkColumn();
urlCol.DataTextField = "Title";
urlCol.DataNavigateUrlField = "Hyperlink";
urlCol.HeaderText = "Title";
// Add three columns to collection.
DefaultGrid.Columns.Add(indexCol);
DefaultGrid.Columns.Add(urlCol);
DefaultGrid.Columns.Add(descCol);
}
Some important observations
|