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

How to get the *actual* data value on ItemDataBound event

1 Answer 1741 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Margo Noreen
Top achievements
Rank 1
Iron
Veteran
Margo Noreen asked on 05 Apr 2012, 03:17 PM
I have wired up an ItemDataBound handler for my radgrid.  Pretty simple standard fare (see code snippet below).  However, I've noticed while stepping through the debugger on this that when the column value that I'm retrieving is an empty string, I'm actually getting back " "... so my code never thinks it's an empty string...  I realize I could code something that looks for " " but that seems fragile.  

So I was wondering how I would access not the value from the radgrid column (which appears to be applying some formatting or whatever), but the actual, true blue underlying value from the data source. 

Any references on how to do so would be greatly appreciated.
 
protected void gvMain_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem gridRow = (GridDataItem)e.Item;
           bool hasEamil = (gridRow["Email"].Text.Length > 0);
       }
   }

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 10 Apr 2012, 02:40 PM
Hello Margo,

Thank you for contacting us.

RadGrid modifies the empty strings since the rendering event requires that the cell value is not null or empty.
In order to get the original value of your cell, you need to access it as a DataKeyValue following these steps:
  • add the column datafield to DataKeyNames of your grid table:
<MasterTableView ... DataKeyNames="YourKeyField,Email">
  • modify your code-behind so you can use the original value:
GridDataItem gridRow = (GridDataItem)e.Item; 
bool hasEmail = gridRow.GetDataKeyValue("Email").ToString() == string.Empty;

I hope this will be helpful. Feel free to turn to us if you need further assistance.

Greetings,

 

Eyup

 

the Telerik team

If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Margo Noreen
Top achievements
Rank 1
Iron
Veteran
Answers by
Eyup
Telerik team
Share this question
or