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

Find value in itemdatabound event

6 Answers 526 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Terry
Top achievements
Rank 1
Terry asked on 01 Jul 2011, 09:50 PM
I have a Radgrid on an ASP.NET form and it is bound to a database table. In the view mode one of the columns has a text value in it that shows a mode. I am using an edit form. In the edit form I have a dropdown on that same field that allows the user to select one of three choices. I can find the dropdown in the edit template using
If TypeOf e.Item Is GridEditFormItem And e.Item.IsInEditMode Then
            dp = e.Item.FindControl("dpRoles")

But what I can't figure how to get is the current data value in the current data row. I know it is in the griddataitem but how do I get to this in the itemdatabound event. I want to "jam" the dropdown list to what is currently in the grid before it goes to edit mode on that row.

Thanks in advance for your help.

Terry

6 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Jul 2011, 06:07 AM
Hi,

method 1 :
<MasterTableView DataKeyNames="ID">
GridEditableItem editItem = (GridEditableItem)item.EditFormItem;
int ID =Convert.ToInt32(dataitem.GetDataKeyValue("ID")); // get old value
DropDown dpRoles = (DropDown)objGridEditableItem.FindControl("dpRoles");
dpRoles.SelectedValue = ID.ToString()

Method 2 :
<asp:Label ID="lblRoles"  runat="server" Text='<%# Eval("RoleID") %>' Visible="False">
<asp:DropDown ID="dpRoles" runat="server">
GridEditableItem editItem = (GridEditableItem)item.EditFormItem;
    DropDown dpRoles = (DropDown)objGridEditableItem.FindControl("dpRoles");
    Label lblRoles = (Label)objGridEditableItem.FindControl("lblRoles");
    dpRoles.SelectedValue = lblRoles.Text.ToString();

If this is not your case, please elaborate the scenario.

Thanks,
Jayesh Goyani
0
Terry
Top achievements
Rank 1
answered on 02 Jul 2011, 03:43 PM
This works perfectly - thanks. I tried the second method but the problem is that I do not have complete control over the data that I am being given and occasionally I can get a null value for the role and there seems to be no graceful way to handle that in the Eval() approach whereas in the code behind I can place some try catch logic to default to a valid dropdown value. Thanks again for the solution.

Terry
0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Jul 2011, 04:59 PM
GridEditableItem editItem = (GridEditableItem)item.EditFormItem;
    DropDown dpRoles = (DropDown)objGridEditableItem.FindControl("dpRoles");
    Label lblRoles = (Label)objGridEditableItem.FindControl("lblRoles");
   if(!String.IsnullorEmpty(lblRoles.Text))
{
    dpRoles.SelectedValue = lblRoles.Text.ToString();
}

Thanks,
Jayesh Goyani

0
Terry
Top achievements
Rank 1
answered on 05 Jul 2011, 11:14 PM
Ok I spoke too soon. This works great while editing an existing record but when I try to add a new record it gives me an error. Item in insert mode does implement indexer only when the edit form is autogenerated...

I sense that it is telling me that the value is not correct during the insert operation but I can not figure out what. The dropdown has the items entered directly in the declaration - there is no data source.

<asp:DropDownList ID="dpItemDisplay" runat="server">
                                        <asp:ListItem Text="9 Items" Value="9"></asp:ListItem>
                                        <asp:ListItem Text="12 Items" Value="12"></asp:ListItem>
                                        <asp:ListItem Text="15 Items" Value="15"></asp:ListItem>
                                        <asp:ListItem Text="18 Items" Value="18"></asp:ListItem>
                                        <asp:ListItem Text="21 Items" Value="21"></asp:ListItem>
                                        <asp:ListItem Text="24 Items" Value="24"></asp:ListItem>
                                        <asp:ListItem Text="27 Items" Value="27"></asp:ListItem>
                                        <asp:ListItem Text="30 Items" Value="30"></asp:ListItem>
                                        <asp:ListItem Text="33 Items" Value="33"></asp:ListItem>
                                        <asp:ListItem Text="36 Items" Value="36"></asp:ListItem>
                                        <asp:ListItem Text="39 Items" Value="39"></asp:ListItem>
                                        <asp:ListItem Text="42 Items" Value="42"></asp:ListItem>
                                        <asp:ListItem Text="45 Items" Value="45"></asp:ListItem>
                                        <asp:ListItem Text="48 Items" Value="48"></asp:ListItem>
                                        <asp:ListItem Text="51 Items" Value="51"></asp:ListItem>
                                    </asp:DropDownList> 

How can I clear this for an insert?

Terry
0
Veli
Telerik team
answered on 11 Jul 2011, 09:26 AM
Hi Terry,

Can you show us the relevant snippet from the codebehind that is causing this exception?

On a side, note, you can bind to a data field from RadGrid's data source in the ItemDataBound event handler as follows:

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs)
    If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
        Dim roleId As Object = DataBinder.Eval(e.Item.DataItem, "RoleID")
        If roleId Is Nothing Or roleId = DBNull.Value Then
            roleId = String.Empty
        End If
         
        DropDown dpRoles = (DropDown)objGridEditableItem.FindControl("dpRoles")
        dpRoles.SelectedValue = roleId.ToString()
    End If
End Sub

Greetings,
Veli
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Terry Harrison
Top achievements
Rank 1
answered on 11 Jul 2011, 06:57 PM
Here is the code I was using
Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgsHandles RadGrid1.ItemDataBound
            Dim theitem As GridEditableItem = Nothing
            If TypeOf e.Item Is GridEditFormItem And e.Item.IsInEditMode Then
                'Dim theitem As GridEditFormItem = e.Item
                theitem = e.Item
                Dim strSelect As String = theitem.Item("itemdisplay").Text
                Dim dlst As DropDownList = theitem.FindControl("dpItemDisplay")
                If dlst IsNot Nothing Then
                    If dlst.Items.FindByValue(strSelect) IsNot Nothing Then
                        dlst.SelectedItem.Selected = False
                        dlst.Items.FindByValue(strSelect).Selected = True
                    End If
                End If
            End If
 
        End Sub


I could tell it was causing issues because it was in insert mode so I added
Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgsHandles RadGrid1.ItemDataBound
            Dim theitem As GridEditableItem = Nothing
            If TypeOf e.Item Is GridEditFormItem And e.Item.IsInEditMode And Not TypeOf e.Item Is GridEditFormInsertItem Then
                'Dim theitem As GridEditFormItem = e.Item
                theitem = e.Item
                Dim strSelect As String = theitem.Item("itemdisplay").Text
                Dim dlst As DropDownList = theitem.FindControl("dpItemDisplay")
                If dlst IsNot Nothing Then
                    If dlst.Items.FindByValue(strSelect) IsNot Nothing Then
                        dlst.SelectedItem.Selected = False
                        dlst.Items.FindByValue(strSelect).Selected = True
                    End If
                End If
            End If
 
        End Sub
By executing the code only when it was in edit mode I was able to make it work.

Tags
Grid
Asked by
Terry
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Terry
Top achievements
Rank 1
Veli
Telerik team
Terry Harrison
Top achievements
Rank 1
Share this question
or