This is a migrated thread and some comments may be shown as answers.
Getting at the data in ItemDataBound
1 Answer 63 Views
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 2
Steve asked on 04 Apr 2010, 11:26 PM
I've always added the bound column definitions to my markup then just accessed them in ItemDataBoud via item["COLUMNNAME"]

Is there some other way to do it without needing to add\reference the columns?  Reason I'm asking is a grid I'm working on has a LOT of columns and Id like to not have to define each (bloaty markup).

Is there a way to just get at that columns data directly in code (without a secondary query on each row)?

Thanks,
Steve

1 Answer, 1 is accepted

Sort by
0
Stacy
Top achievements
Rank 1
answered on 05 Apr 2010, 05:48 AM
Hi Steve,

I am the completely different developer where I don't hard code anything in my markup which provides my code flexibility and the ability to be dynamic.  In my code I set this value MasterTableView.AutoGenerateColumns="true" and then columns are automatically generated.  You don't have to put column definitions in the markup or define BoundColumns at run-time either they just magically show up.  This is a great concept for applications such as reports that do not have a set/defined number of columns or even column names.

With this method, it's great because I use the reverse thinking which is "show all my columns automatically but, then hide the ones I don't want to see."  I have a seperate xml file which stores column names and whether I do not want to display them.  e.g. <Column Name="Dept ID" Display="false" />

To reference the columns in the _ItemDataBound event, simple use the .RenderColumns property to reference the data.  e.g. 

        {  
                // Box a the ItemType as it is easier to use  
                GridDataItem gridDataItem = (GridDataItem)newGridItem.Item;  
 
                // Iterate thorugh every column and format the value  
                foreach (GridColumn gridColumn in radGrid.MasterTableView.RenderColumns)  
                {  
                    if (gridColumn.UniqueName == "Sales"// Your Column Name definition from you xml file for example, it's hardcoded here as example  
                    {  
                        // Your code to modify the "Sales" column for this specific Item (Row)  
                    }
                }
        }  

Make note, read the definition of the property becuase it behaves a little differently than just referencing the column name directly.

Hope this helps.

Stacy
Tags
Grid
Asked by
Steve
Top achievements
Rank 2
Answers by
Stacy
Top achievements
Rank 1
Share this question
or