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

Naming columns when auto-generating columns at runtime

2 Answers 72 Views
Grid
This is a migrated thread and some comments may be shown as answers.
bdrennen
Top achievements
Rank 1
bdrennen asked on 15 Apr 2010, 11:46 PM
I have a grid that I'm populating with the contents of datasource1. Because the select statement for datasource1 changes, I never know what columns I'm going to be getting when the grid is populated with data when the page loads. One unfortunate result of this is that the columns don't get named.

How would I go about assigning names to the columns? All I need to do is give the columns in the grid the same name as the columns in the select statement.

Thank you!

Bryan

2 Answers, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 20 Apr 2010, 03:01 PM
Hello Bryan,

What do you mean by "column name" - is it the UniqueName or the HeaderText? For autogenerated columns, both of these assume the name of the DataField (from the SELECT statement). If you want to change them, you can use the ColumnCreated server event.

http://www.telerik.com/help/aspnet-ajax/grdusingcolumns.html

(see "Customizing Columns Programmatically")

Greetings,
Dimo
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
bdrennen
Top achievements
Rank 1
answered on 20 Apr 2010, 04:52 PM

I was having trouble getting the unique name of the columns in order to toggle the display value for the columns. Sorry for not being more clear in my question. I was trying to do the following:

 

 

 

 

foreach (GridColumn column2 in RadGrid1.Columns)  
{  
    if (column.UniqueName == e.Item.Text)   
    {  
        if (column.Visible) column.Display = !column.Display;  
    }  
}  
 

 

 

It wasn't working because the column collection didn't have anything in it. However, I found out that if I use the RadGrid1.MasterTableView.RenderColumns array instead of the RadGrid1.Columns collection, I can get the unique names for each of the columns in the grid.

foreach (GridColumn column in RadGrid1.MasterTableView.RenderColumns)  
{  
    if (column.UniqueName == e.Item.Text)   
    {  
        if (column.Visible) column.Display = !column.Display;  
    }  

Thanks!

Bryan

Tags
Grid
Asked by
bdrennen
Top achievements
Rank 1
Answers by
Dimo
Telerik team
bdrennen
Top achievements
Rank 1
Share this question
or