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

How to check the existence of a column

5 Answers 475 Views
Grid
This is a migrated thread and some comments may be shown as answers.
François
Top achievements
Rank 1
François asked on 29 Oct 2008, 09:19 AM
Hi every body.

I don't find how to determine if a column with a specific UniqueName exists in a ItemDataBound. My example here:

TableCell oCellStatus = null
try 
   oCellStatus = oGridDataItem["StatusColumn"]; 
catch 
   oCellStatus = null
if (oCellStatus != null) 
   Image oImageStatus = new Image(); 
   oImageStatus.ImageUrl = String.Format("~/App_Themes/{0}/Images/{1}", this.Page.Theme, "Status-" + oCellStatus.Text + ".png"); 
   oCellStatus.Controls.Add(oImageStatus); 


You see that I use a try catch to determine if a specific column exists in my GridDataItem. I don't find a Exists(UniqueName) or collections to loop all columns. Is there a solution to this specific question ?

5 Answers, 1 is accepted

Sort by
0
Missing User
answered on 29 Oct 2008, 10:16 AM
Hello François,


Please find attached a sample web application that demonstrates the needed approach.

Best wishes,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
François
Top achievements
Rank 1
answered on 29 Oct 2008, 10:47 AM
In fact, I can't use RadGrid object because my code is in code behind of a masterpage which loop all controls. And when it find a RadGrid, it add a ItemDataBound event which manipulate row. This allow my application to define status image for all DataGrid in content.
So, with GridDataItem, can I loop all columns with its UniqueName ?
0
Accepted
Missing User
answered on 29 Oct 2008, 10:58 AM
Hi François,


You can use the "sender" argument to obtain the Grid. Here is an example:

    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        RadGrid radGrid = (RadGrid)sender; 
    } 




Greetings,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
François
Top achievements
Rank 1
answered on 29 Oct 2008, 11:33 AM
Thanks ...
I'm confuse, ridiculous, dizzy ...
0
Missing User
answered on 29 Oct 2008, 05:10 PM
Hi François,

I apologize for the confusion.

I created for you two simple pages which demonstrate two approaches to accomplish your task. Please, find them attached.

Default.aspx: The first approach is to use the FindByUniqueNameSafe method. The FindByUniqueNameSafe(columnName) performs the same task as GetColumnSafe(columnName) method. Here is an example:

            Object MyColumnName = 
                radGrid.MasterTableView.Columns.FindByUniqueNameSafe("MyColumnName"); 
 
            if (MyColumnName != null
                //MyColumnName column exists 


Default1.aspx: Another approach would be to use the RenderColumns collection. The RadGrid1.MasterTableView.RenderColumns collection gives you access to all columns in the RadGrid - you can use it to iterate through all columns.

        if (e.Item is GridDataItem) 
            foreach (GridColumn gridColumn in radGrid.MasterTableView.RenderColumns) 
                Response.Write("<br />" + gridColumn.UniqueName); 


Let me know if that helps.

Greetings,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
François
Top achievements
Rank 1
Answers by
Missing User
François
Top achievements
Rank 1
Share this question
or