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

Checking if column exists in GridDataItem

2 Answers 342 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eliyahu Goldin
Top achievements
Rank 1
Eliyahu Goldin asked on 05 Sep 2008, 01:13 PM

This code will throw an exception:

                        GridDataItem dItem = e.Item as GridDataItem;  
                        // rename edit button  
                        TableCell cell = dItem["cmdEdit"];  
                        if (cell != null)  
                        {  
                            Control control = cell.Controls[0];  
                            if (control != null)  
                            {  
                                LinkButton cmdEdit = control as LinkButton;  
                                cmdEdit.Text = "View";  
                            }  
                        }  
 

Instead of handling exceptions, I would rather prefer to check if the column exists prior to accessing it. I couldn't find any simple way of checking column existense by unique name.

Why don't you introduce a method GridDataItem.ContainsColumn(uniqueName) ?

2 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 05 Sep 2008, 01:24 PM
Hello Eliyahu,

You can use RadGrid1.MasterTableView.GetColumnSafe(); in order to check if the given column exist.

Also you to modify your code a bit:
if (cell.HasControls()) 
    Control control = cell.Controls[0]; 

This will prevent exceptions when there are no controls in the current cell.

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Eliyahu Goldin
Top achievements
Rank 1
answered on 07 Sep 2008, 07:02 AM
Thanks, Daniel.
Tags
Grid
Asked by
Eliyahu Goldin
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Eliyahu Goldin
Top achievements
Rank 1
Share this question
or