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

Access each column in a radgrid and set ItemStyle.Width (ItemDatabound)

5 Answers 809 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nicklas
Top achievements
Rank 1
Nicklas asked on 25 Jun 2013, 08:38 AM
Hello!

I'm having problems with accessing a radgrids columns manually added from source code, I have loads of columns, and I don't want to repeat myself, adding the Width and change the headertext, according to my algorithm manually on each column.

Therefore I've been trying to find a way to set them automatically in the codebehind, however I am having problems accessing all the columns and settings. 

The columns is as said not generated automatically.

Any idea?

Before When I have had the columns auto generated I have easily achieved this by using the Event ColumnCreated

protected void radGrid_ColumnCreated(object sender, GridColumnCreatedEventArgse)
{
                e.Column.HeaderStyle.Width = 200;
                e.Column.ItemStyle.Width = 200;
                e.Column.HeaderText = "test";
               e.Column.UniqueName = "test2"
}

Now I want to do the same but with manually added columns? (The ColumnCreated event is only triggered when the columns are auto generated, sadly =()

So something like this, by what I assume listening to the ItemDataBound event?

protected void radGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
      //Set column Width and Height and also the columns Header text and Unique name.
 }



Appreciate any help :) Hope I made it clear enough to understand :) 

Thanks in advance! ( Couldnt find any threads similair that accessed the HeaderStyle nor the UniqueName properties of a column)

5 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 25 Jun 2013, 09:25 AM
Hi Nicklas,

I guess you want to set the properties for the GridBoundColumn from code behind, please use the PreRender event.
Please try the following code snippet.

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)          
    {
        foreach (GridColumn col in RadGrid1.Columns)
        {
          col.HeaderStyle.Width = 600;
          col.ItemStyle.Width = 600;
            if (col.UniqueName == "UniqueName")
             {
                col.HeaderText = "aa";
             }
          RadGrid1.Rebind();
        }
    }

Note:
Please set the TableLayout="Fixed"  for the MasterTable.
 
Thanks,
Shinu
0
Nicklas
Top achievements
Rank 1
answered on 25 Jun 2013, 10:23 AM
Lovely, thanks alot! It worked out perfect! 

I have one more question regarding the grid, I do like to access the dataField property from the PreRender ( I want to enter as minimal code as possible in source code for the columns and have the DataField value as base for the other properties)

Eg. I have a function that converts FACTORY_ADDRESS (which is the Datafield) to Factory Address as Header text.

<telerik:GridBoundColumn DataField="FACTORY_ADDRESS" HeaderText="Factory Address">
 </telerik:GridBoundColumn>

So I really need to get column's DataField value somehow if possible? (Seems like it should be possible)
0
Nicklas
Top achievements
Rank 1
answered on 25 Jun 2013, 10:36 AM
I don't mean to spam, but I recently read that if I do not specify a uniqueName the item will take the dataField as uniqueName?

I gotta go ahead and try it, but if somebody knows it would help if someone can either confirm or deny it :)
0
Nicklas
Top achievements
Rank 1
answered on 25 Jun 2013, 10:43 AM
I don't mean to spam, but I recently read that if I do not specify a uniqueName the item will take the dataField as uniqueName?

I gotta go ahead and try it, but if somebody knows it would help if someone can either confirm or deny it :) 

EDIT:

Okey, Ive tested it by putting a breakpoint in the loop and using the immediate window to find out the value of column.UniqueName, seems like what I read was correct.

If you do not specifiy a unique name, it will inherit the value of DataField. 

So problem solved since I can easily acess the unique name from the code!

Thanks again Telerik community, you never seem to let one down :)
0
Nicklas
Top achievements
Rank 1
answered on 25 Jun 2013, 10:44 AM
Okey, Ive tested it by putting a breakpoint in the loop and by using the immediate window to find out the value of column.UniqueName, seems like what I read was correct.

If you do not specifiy a unique name, it will inherit the value of DataField. 

So problem solved since I can easily acess the unique name from the code!

Thanks again Telerik community, you never seem to let one down :)
Tags
Grid
Asked by
Nicklas
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Nicklas
Top achievements
Rank 1
Share this question
or