Hello, I´m using a RadGrid for displaying data bound using the "NeedDataSource"-event. It works almost perfectly. Since I want a combobox in one of the columns, i have a GridTemplateColumn which looks like this:
<
telerik:GridBoundColumn
DataField
=
"CommunicationModule"
HeaderText
=
"CommunicationModule"
UniqueName
=
"CommunicationModule"
></
telerik:GridBoundColumn
>
<
telerik:GridTemplateColumn
DataField
=
"Module"
HeaderText
=
"Module"
UniqueName
=
"Module"
ItemStyle-Width
=
"400px"
>
<
ItemTemplate
>
<%#DataBinder.Eval(Container.DataItem, "Module")%>
</
ItemTemplate
>
<
EditItemTemplate
>
<
telerik:RadComboBox
runat
=
"server"
ID
=
"cboModule"
>
</
telerik:RadComboBox
>
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
The first column holds the database value (int) and the second column contains the combobox. My plan is to use the first column (which in the end is supposed to be hidden) to set the SelectedValue of the combobox.
I´m using the ItemDataBound event to detect when to populate the combobox and set the selected item. The problem is that every time I try to access the cell value of the "CommunicationModule" cell, it contains  . I try to do this using the e.item["CommunicationModule"].Text.
Since I populate the grid using entity framework and a anonymous list created by a query, I cannot directly cast the e.Item.DataItem either. In that case I need to create a helper class that matches the signatures of my query and that would create much more work for me since I´m doing several pages with grids populated with different data. The Grid is in "EditForms" Editmode
So:
protected
void
RadGridCU_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item.IsInEditMode)
{
if
(!(e.Item
is
IGridInsertItem))
//Is this a new item?
{
GridEditFormItem item = (GridEditFormItem)e.Item;
RadComboBox cmb = (RadComboBox)item.FindControl(
"cboModule"
);
cmb.DataSource =
//Here I have logic to set the datasource (works);
cmb.DataTextField =
"Name"
;
cmb.DataValueField =
"OID"
;
//I tried to use
//item["CommunicationModule"].Text.ToString();
//But to get it to work, I needed to create helper class and then cast the dataitem..
cmb.SelectedValue = ((MyGridHelperClass)e.Item.DataItem).CommunicationModule.ToString();
cmb.DataBind();
}
}
}
When using EditForms, I can access the value using
item.ParentItem["CommunicationModule"].Text.ToString();
But when using InForms edit, I cannot access the value using item["CommunicationModule"].Text.ToString().