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

How to access CssClass of the column for specific cell??

4 Answers 277 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alexander
Top achievements
Rank 1
Alexander asked on 21 Oct 2009, 03:58 PM

I have columns with defined CssClass, like this one, for instance:

<telerik:GridBoundColumn HeaderStyle-CssClass='GridHeaderGreen ca' ItemStyle-CssClass='ca' HeaderText='Status' SortExpression='Wizard.AlertStatus.Name' DataField='Status.Name' />

In some cases I want to override the look of row cells for every column, i.e. i need to take ItemStyle.CssClass for the column and complement it with some additional class:

protected void rgAlertConfigView_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if ((e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem) && e.Item.DataItem != null)
{

....
for (var i = 0; i < item.Cells.Count; i++)
{
item.Cells[i].CssClass = String.Format("{0} {1}", item.OwnerTableView.Columns[i].ItemStyle.CssClass, [some class depending on data in the row]); 
}
}

rgView_ItemCreated(sender, e);
}

This stuff does not work, because there is some mess: item.Cells.Count = 12, item.OwnerTableView.Columns.Count = 10 (this is correct value) and there is no straight correspondence between Cell and Column and I cannot see how to establish it (?)

Every cell should be able to 'know' what is its column style.

p.s. Thanks to myself! At last I found solution:

for (var i = 0; i < item.OwnerTableView.Columns.Count; i++)
{
item[item.OwnerTableView.Columns[i]].CssClass = String.Format("{0} {1}{2}", item.OwnerTableView.Columns[i].ItemStyle.CssClass, item[item.OwnerTableView.Columns[i]].CssClass, dimmed);
}

4 Answers, 1 is accepted

Sort by
0
BaiH
Top achievements
Rank 1
answered on 27 Oct 2009, 07:48 AM
As long as I'm aware of, it's far better to use column's UniqueName instead of accessing it by its index.
Oh and it is always better to check the docs first ;) Here you go:
Accessing cells and rows
Conditional Formatting for rows/cells on ItemDataBound

--BH
0
Alexander
Top achievements
Rank 1
answered on 27 Oct 2009, 08:24 AM
Dear BaiH,
be sure, i did read doc and i do know what is uniquename and i've tried to use it in my case.
But I don't have idea how to loop via all uniquenames of my columns - i can't see the in the doc.
Or you suggest me, if i have 20 columns in the grid, write such long code:

column[uniquename00]...
....
column[uniquename19]...

?
0
Princy
Top achievements
Rank 2
answered on 27 Oct 2009, 09:30 AM
Hello Alexander,

You can try out the following code to loop through the columns in the grid and set the required css class for each cell:
c#:
protected void rgAlertConfigView_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
       if(e.Item is GridDataItem) 
       { 
           GridDataItem item = (GridDataItem)e.Item; 
           foreach (GridColumn col in rgAlertConfigView.MasterTableView.RenderColumns) 
           { 
               item[col.UniqueName].CssClass = "myClass"
           } 
       } 
   } 

Thanks
Princy.
0
Alexander
Top achievements
Rank 1
answered on 28 Oct 2009, 07:59 AM
Good approach, thanks :)
Tags
Grid
Asked by
Alexander
Top achievements
Rank 1
Answers by
BaiH
Top achievements
Rank 1
Alexander
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or