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

Cannot access cell value in ItemDataBound

7 Answers 359 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 08 Jan 2013, 07:35 AM

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 &nbsp. 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();
            }
        }
    }



How can I really access the cell value without the need of accessing the DataItem?



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().


7 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 10 Jan 2013, 04:20 AM
Hi,

Try the following code to access columns in edit mode.
C#:
protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem item = (GridEditableItem)e.Item;
        TextBox txt = (TextBox)item["CommunicationModule"].Controls[0];
    }
}

Thanks,
Shinu
0
Martin
Top achievements
Rank 1
answered on 10 Jan 2013, 03:04 PM
Hi, thank you for your answer. Your solution works as expected for a GridBoundColumn.

However, for my template column which is bound to an EntityFramework 5 Enum datatype (byte), I have no idea how to solve my problem. In the ItemDataBound event, I have a problem setting the sleected item. My column looks like this

<telerik:GridTemplateColumn DataField="CommunicationDirection" HeaderText="Direction" UniqueName="CommunicationDirection" ItemStyle-Width="400px">
                           <ItemTemplate>
                               <%#DataBinder.Eval(Container.DataItem, "CommunicationDirection")%>
                           </ItemTemplate>
                           <EditItemTemplate>
                                <telerik:RadComboBox runat="server" ID="cboCommunicationDirection">
                                    <Items>
                                        <telerik:RadComboBoxItem Text="In" Value="0"/>
                                        <telerik:RadComboBoxItem Text="In and Out" Value="1"/>
                                        <telerik:RadComboBoxItem Text="Out" Value="2"/>
                                    </Items>
                           </telerik:RadComboBox>
                           </EditItemTemplate>
                       </telerik:GridTemplateColumn>

And I don´t know how to access the value in ItemDataBound...What is the standard way of accessing selected values of Enum Types from Entity Framework? How can I format the visible cell value in the grid? As it is now, it displays the Enum value like My_Ef5_Enum.

Thanks
0
Maria Ilieva
Telerik team
answered on 15 Jan 2013, 10:59 AM
Hello Martin,

Try to access the ComboBox in the template column in the ItemDataBound event like this:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode )
          {            
             GridEditableItem editItem = e.Item as GridEditableItem;
             RadComboBox dropdown = (RadComboBox )editItem.FindControl("cboCommunicationDirection");
             
          }
    }


Greetings,
Maria Ilieva
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.
0
Martin
Top achievements
Rank 1
answered on 15 Jan 2013, 12:24 PM
Hello and thank your for your help. Unfortunately part of my question was how to modify the SelectedValue of the ComboBox. The datasource column is an ENtity Framework 5 Enum-column (byte).
0
Maria Ilieva
Telerik team
answered on 18 Jan 2013, 01:55 PM
Hi Martin,

I'm not  completely sure if I correctly understand your requirements and the issues you are facing in their implementation. Are the item in the ItemTemplate represents the text of the enum field instead of showing bytes? What is the exact selected value you need to set for the edit item and are you facing issues in parsing the value to enum?

Greetings,
Maria Ilieva
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.
0
Martin
Top achievements
Rank 1
answered on 18 Jan 2013, 02:14 PM
Hi, the enum text value is shown in the grid. For example "IN_AND_OUT". The first thing I want to do is to customize the text to "In and Out", secondly, when starting inedit mode, I dont know how to set the "SelectedValue".
0
Jayesh Goyani
Top achievements
Rank 2
answered on 21 Jan 2013, 11:12 AM
Hello,

Please check below link.

How to use special characters in enums in asp.net

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Martin
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Martin
Top achievements
Rank 1
Maria Ilieva
Telerik team
Jayesh Goyani
Top achievements
Rank 2
Share this question
or