This is a migrated thread and some comments may be shown as answers.

Properties of autogenerated columns

5 Answers 138 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Christian
Top achievements
Rank 1
Christian asked on 30 Nov 2011, 04:01 PM
Hi,

I'm trying to create a grid of variable size based on the datatable that feeds it.

Currently the datatable consists of a first static column, and then any number of dynamically generated columns. I would like to be able to set different properties for the static and dynamic columns (size, headings, alignment etc).

What's more, I'd like to be able to add some code triggered by clicking on the column headers (only the dynamic ones).

As it stands I'm just using autogeneratecolumns="true" to create a grid that matches the data, but I fear that this will not give me the power I need to add this more complex functionality. Am I right? How would be best to go about this?

Is there a way to set the properties of autogenerated columns to the extent I require? If so I did think to maybe split the datatable and populate the static column with its specific properties separately from the dynamic ones (at least they would share the same properties) - although this would then involve mixing autogenerate/static generation and multiple data sources which might be messy in itself. I'm on something of a deadline hence why I can't just try all possibilities in the hope that one works :)

Apologies for this somewhat rambling post, but any insight into this matter would be greatly appreciated.

Cheers.

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 01 Dec 2011, 08:09 AM
Hello Christian,

You can customize the properties of auto-generated columns at runtime by attaching the ColumnCreated event. This enables to customize the column accordingly. Also check the following help documentation.
Using Columns

-Shinu.
0
Christian
Top achievements
Rank 1
answered on 01 Dec 2011, 01:48 PM
Hi, thanks for your reply.

I did find out that you can set the properties of auto-generated columns in either prerender or column created, however what I would like to do is associate custom code with the generated column header buttons.

I know you can set the header button type, but I can't figure out how to set the properties. Link buttons support custom commands, so I set the button header type accordingly:

                GridBoundColumn boundColumn = e.Column as GridBoundColumn;
                boundColumn.HeaderButtonType = GridHeaderButtonType.LinkButton;

How would one go about adjusting the CommandName of said button?

Thanks.
0
Shinu
Top achievements
Rank 2
answered on 02 Dec 2011, 04:54 AM
Hello Christian,

Try the following code.
C#:
protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
{
 if (e.Column is GridButtonColumn)
  {
    GridButtonColumn col = e.Column as GridButtonColumn;
    col.HeaderButtonType = GridHeaderButtonType.LinkButton;
    col.CommandName = "Delete";
  }
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
 if (e.CommandName == "Delete")
 {//handling in ItemCommand
 }
}

-Shinu.
0
Christian
Top achievements
Rank 1
answered on 02 Dec 2011, 10:52 AM
Hi,

Unfortunately, I'm auto-generating the columns from a DataTable and so I believe the columns created are GridBoundColumns. With GridBoundColumns it seems you can set the header type to LinkButton, but it does not then have the CommandName property. Is there some way around this?

Cheers.
0
Tsvetina
Telerik team
answered on 07 Dec 2011, 07:54 AM
Hello Christian,

You can change the button type on ColumnCreated and then on ItemCreated change the CommandName of the button.
protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
{
    if (e.Column is GridBoundColumn)
    {
        e.Column.HeaderButtonType = GridHeaderButtonType.LinkButton;
        
    }
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridHeaderItem)
    { //col unique name will be the same as the field name
        ((e.Item as GridHeaderItem)["CategoryID"].Controls[0] as LinkButton).CommandName = "Custom";
    }
}

However, with changing the command name, you will lose the sorting capability of the columns. So, you can also consider leaving the default command name (Sort) and just perform your custom logic when ItemCommand fires with e.CommandName equal to RadGrid.SortCommandName.

Kind regards,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Christian
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Christian
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or