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

5 posts, 0 answers
  1. Alexander
    Alexander avatar
    152 posts
    Member since:
    Jun 2012

    Posted 21 Oct 2009 Link to this post

    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);
    }

  2. BaiH
    BaiH avatar
    83 posts
    Member since:
    Aug 2008

    Posted 27 Oct 2009 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Alexander
    Alexander avatar
    152 posts
    Member since:
    Jun 2012

    Posted 27 Oct 2009 Link to this post

    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]...

    ?
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 27 Oct 2009 Link to this post

    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.
  6. Alexander
    Alexander avatar
    152 posts
    Member since:
    Jun 2012

    Posted 28 Oct 2009 Link to this post

    Good approach, thanks :)
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017