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

[Solved] How to read directly from data source in custom data binding

3 Answers 225 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 27 Feb 2008, 08:49 PM
I'm overriding the ItemDataBound event in a user control that contains a Prometheus RadGrid, and I want to perform some custom formatting of the data.  I'm having a bit of a challenge, though, because I can't figure out how to pull the raw data out of the "current" row of the MasterTableView.DataSource.

I'm looking at the "Conditional Formatting for rows/cells on ItemDataBound" article in the help, and I see this example:

If (Integer.Parse(dataBoundItem( "Size" ).Text) > GridItemType.Footer) Then  
    dataBoundItem( "Received" ).ForeColor = Color.Red  
    dataBoundItem( "Received" ).Font.Bold = True  
    'Customize more...  
End If 

I want to do something very similar, but with one exception...   Where this example reads the text from one of the other table cells, I want to read the raw data object out of the data source.  With RadGrid adding so much intelligence around the variety of objects it will accept as the DataSource, how can I reliably find the data object that relates to one of the field names that I earlier added to AdditionalDataFieldNames for the row/object that's currently being bound?

3 Answers, 1 is accepted

Sort by
0
Steve
Top achievements
Rank 1
answered on 28 Feb 2008, 03:52 PM
Update...

After further research, I've found something that seems like it *ought* to work, but isn't...   Inside my Grid_ItemDataBound(object sender, GridItemEventArgs e), I have...

if (e.Item as GridDataItem != null)  
{  
    //Get the instance of the right type  
    GridDataItem dataBoundItem = e.Item as GridDataItem;  
    object data_item = dataBoundItem.DataItem;  
    Hashtable row_object = new Hashtable();  
    e.Item.OwnerTableView.ExtractValuesFromItem(row_object, dataBoundItem);  
 
    ...  
}  
 

So now, my Hashtable, row_object, does have one entry for each property in my object.  The problem, though, is that every single one of the entries has a value of null.  I know the actual source object has a value, because the TableCell does have valid text in it from that original source.

I believe I'm *soooo* close, but those nulls are killing me.  What is wrong with my call to ExtractValuesFromItem() ?
0
Steve
Top achievements
Rank 1
answered on 28 Feb 2008, 08:44 PM
SOLVED: Never did figure out the issue with ExtractValuesFromItem(), but I did finally figure out the solution:

protected void Grid_ItemDataBound(object sender, GridItemEventArgs e)  
{  
    if (e.Item as GridDataItem != null)  
    {  
        //Get the instance of the right type  
        GridDataItem dataBoundItem = e.Item as GridDataItem;  
        object data_item = dataBoundItem.DataItem;  
        string data_field_name = "MyFieldName";  
 
        object obj = System.Web.UI.DataBinder.GetPropertyValue(data_item, data_field_name);  
 
    }  
0
Jimmy Stuart
Top achievements
Rank 1
answered on 05 Mar 2008, 07:40 PM
Another way that I like to use that might be a little more simple for you is to create a public static/shared function in the code behind of the page that returns the type of value needed for the control (say string) and use that in conjunction with the databinding:

<asp:Label id="Label1" text='<%# MyFormattingFunction(Eval("MyDbField")) %>' runat="server"></asp:Label>
Tags
Grid
Asked by
Steve
Top achievements
Rank 1
Answers by
Steve
Top achievements
Rank 1
Jimmy Stuart
Top achievements
Rank 1
Share this question
or