|
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
In previous
articles we have limited the discussion to very elementary use of Can I select the columns, from the datasource, which should be rendered?
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 article 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
When you bind a data source to
There are 5 types of columns that you can add to
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
Back to our original question. How do we control what columns to display? Here
are the steps that you need to follow to accomplish this. First we will show
this can be done using
Now we will show how this can be accomplished programmatically in a code behind
file. Now you don't have to supply any
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 observationsAutoGenerateColumns value should be set before you bind datasource to DataGrid controlIf you are using design time method of specifying columns, then this is not a problem because you will specifying this value in
DefaultGrid.AutoGenerateColumns = false;
DefaultGrid.DataBind();
Not Setting the AutoGenerateColumns AttributeIf you don't set BoundColumn Control Order Is ImportantOrder in which Empty Columns Collection
Explicitly Added and Automatically Added Column CombinationsIf you use combination of explicitly added columns and automatically generated
columns, then explicitly declared columns are rendered first. This
means that you have set HeaderText property and explicitly added columnsIf you explicitly add columns then set value of Rendering properties specified for explicitly added columnsRendering properties specified for explicitly added columns, only affects the
column and not the contained HTML tag. What this means is that if you
set the value of any property like
|
||||||||||||||||||||||||||||||||||||||||||||