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

Formatting the Child Cells of a Hierarchical RadGridView

1 Answer 71 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jeremy Murtishaw
Top achievements
Rank 1
Jeremy Murtishaw asked on 20 May 2009, 11:42 PM
Hello,

I'm trying to format the Child Cells of a Hierarchical GridView via the CellFormatting event.

Here's the code I'm trying to use:

        private void grdRecord_CellFormatting(object sender, CellFormattingEventArgs e) 
        { 
            if (e.CellElement is GridDetailViewCellElement) 
            { 
                if (e.CellElement.ColumnIndex == (int)RecordScorpion.Column.Network) 
                { 
                    if (e.CellElement.Text.ToString().Contains("-")) 
                    { 
                        grdRecord.Rows[e.CellElement.RowIndex].Cells[(int)Record.Column.Value].CellElement.ForeColor = Color.Blue; 
                    } 
                    else if (e.CellElement.Text.ToString() != string.Empty) 
                    { 
                        grdRecord.Rows[e.CellElement.RowIndex].Cells[(int)Record.Column.Value].CellElement.ForeColor = Color.Purple; 
                    } 
                } 
                else if (e.CellElement.ColumnIndex == (int)RecordScorpion.Column.Item) 
                { 
                    e.CellElement.Text = ""
                } 
            } 

However, I'm finding that while the cellelement type sometimes is GridDetailViewCellElement, the columnindex is typically negative. In addition, I'm finding that the rowindex is out of range as well. Any help would be appreciated.

Thanks!
Jeremy


1 Answer, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 21 May 2009, 11:41 AM
Hello Jeremy,

GridDetailViewCellElement is used to hold the child grid element. However, it isn't a data cell and it doesn't contain any data. Only data cells have valid column and row indexes. You should check whether the cell is a GridDataCellElement. To be sure that this is a cell from a child view, check its ViewTemplate property. Here is the modified code:

private void grdRecord_CellFormatting(object sender, CellFormattingEventArgs e) 
    if (e.CellElement is GridDataCellElement  
        && e.CellElement.ViewTemplate == this.radGridView1.MasterGridViewTemplate.ChildGridViewTemplates[0]) 
    { 
        if (e.CellElement.ColumnIndex == Record.Column.Value) 
        { 
            string scorpionValue = e.CellElement.RowInfo.Cells[(int)RecordScorpion.Column.Network].Value.ToString(); 
            if (scorpionValue.Contains("-")) 
            { 
                e.CellElement.ForeColor = Color.Blue; 
            } 
            else if (scorpionValue != string.Empty) 
            { 
                e.CellElement.ForeColor = Color.Purple; 
            } 
            else 
            { 
                e.CellElement.ForeColor = Color.Black; 
            } 
        } 
        else if (e.CellElement.ColumnIndex == (int)RecordScorpion.Column.Item) 
        { 
            e.CellElement.Text = ""
        } 
    } 

I hope this helps. If you have any additional questions, feel free to write us.

Regards,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
Jeremy Murtishaw
Top achievements
Rank 1
Answers by
Jack
Telerik team
Share this question
or