Iterate through rows/cells and get header text of cell

5 posts, 1 answers
  1. jfkrueger
    jfkrueger avatar
    269 posts
    Member since:
    Jul 2012

    Posted 14 Nov 2008 Link to this post

    I need to iterate through the rows and cells from code once the grid is already created and get the header text of each cell. How can this be accomplished?

    Thanks!
  2. Kevin Babcock
    Kevin Babcock avatar
    189 posts
    Member since:
    Mar 2007

    Posted 16 Nov 2008 Link to this post

    Hello Joe,

    If all you want is the header text for your cells, then you can simply iterate through the columns of the RadGrid and retrieve their header text values. All cells in a column will have the same header text value.

    Here is a simple way to do that after the RadGrid.OnDataBound event is fired (which occurs after the data source has been bound to the grid):

    protected void RadGrid1_DataBound(object sender, EventArgs e) 
        foreach (Telerik.Web.UI.GridColumn column in RadGrid1.Columns) 
        { 
            var headerText = column.HeaderText; 
     
            // Put the rest of your logic here... 
        } 

    I hope this helps. If you have further questions please let me know.

    Regards,
    Kevin Babcock
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. jfkrueger
    jfkrueger avatar
    269 posts
    Member since:
    Jul 2012

    Posted 16 Nov 2008 Link to this post

    Thanks, that helps but I still need to be able to do it as I am iterating through the grid. Here is what I have come up with so far but there are still problems:

    If rgCoverages.Items.Count > 0 Then 
     
         gvRow = rgCoverages.items(rgCoverages.Items.Count - 1)  
     
         For Each myCell As Telerik.Web.UI.GridTableCell In gvRow.Cells  
     
              strHeader = rgCoverages.Columns(intCellIndex).HeaderText    
     
              intCellIndex += 1  
        
         Next 
     
    End If 
     
     

    Here I am trying to grab the last row in the gridview, iterate through the cells to find out which cells have checkboxes (i removed the code where I am trying to find the checkboxes because I need to get past this issue first) in them and then to find the header text of these cells so I know which type of coverage it represents (medical, dental, vision etc.) and finally to determine if the checkbox was checked. The grid is created dynamically and could have any number of combinations for coverages offered so I can't count on it always having the same structure.

    The problem with the above code is that somehow there are more cells in this row then there are columns in the grid. I don't know why but for some reason it says that there are 8 cells in the row but only 6 columns in the grid, so I get an argument out of range exception.

    Thanks again!
  5. Answer
    Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 17 Nov 2008 Link to this post

    Hello Joe,

    Please use RenderColumn collection instead Columns.

    Kind regards,
    Vlad
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. jfkrueger
    jfkrueger avatar
    269 posts
    Member since:
    Jul 2012

    Posted 17 Nov 2008 Link to this post

    Ok, got it to work using MasterTableView.RenderColumns collection.

    Thanks!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017