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

How to bound RadComboBox in FormEdit mode

5 Answers 87 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andris
Top achievements
Rank 1
Andris asked on 04 Aug 2012, 11:18 PM
I'm trying to populate a DropDownList on my editform (automatically generated), the only fancy thing is that I have 4 options to choose from and i dont want to create a table in a db just to bind it to that.

 <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellSpacing="0" GridLines="None" PageSize="20" Skin="Forest">
                   <Columns>
<telerik:GridBoundColumn HeaderText="Discount" UniqueName="ColDiscount" />
        <telerik:GridDropDownColumn HeaderText="
Type" UniqueName="ColDType" >
</Columns>    
 </telerik:RadGrid>


CodeBehind:

Protected Sub RadGrid1_onitemdatabound(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If e.Item.IsInEditMode Then
            Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
            If Not (TypeOf e.Item Is IGridInsertItem) Then
                Dim combo As RadComboBox = DirectCast(item.FindControl("ColDType"), RadComboBox)
                Dim selectedItem As New RadComboBoxItem()
                selectedItem.Text = DirectCast(e.Item.DataItem, DataRowView)("ColDType").ToString()
                selectedItem.Value = DirectCast(e.Item.DataItem, DataRowView)("ColDType").ToString()
                selectedItem.Attributes.Add("coldtype", DirectCast(e.Item.DataItem, DataRowView)("ColDType").ToString())
                combo.Items.Add(selectedItem)
                selectedItem.DataBind()
            End If
        End If
    End Sub



i tried this.
I tried populating it in the handler of the RadGrid1.ItemDataBound event and tried to find the dropdown control with DirectCast(item.FindControl("ColDType"), RadComboBox), but it came back as null so yeah, thats a thing. It couldn't find a ComboBox named ColDType on the autogenerated edit forms. Thats my best guess.

Your help is appreciated, thanks.

5 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 05 Aug 2012, 01:42 PM
Hello,

Please try with below code snippet.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem editItem = (GridEditableItem)e.Item;
                GridDropDownListColumnEditor dropDownEditor = (GridDropDownListColumnEditor)editItem.EditManager.GetColumnEditor("ColDType"); // columnuniquename
                //dropDownEditor.ComboBoxControl -- Access your control here
                //dropDownEditor.DropDownListControl -- Access your control here
            }
        }

Note : For Editmode="FormEdit" use GridEditFormItem
Thanks,
Jayesh Goyani
0
Andris
Top achievements
Rank 1
answered on 08 Aug 2012, 07:42 PM
Thank you, this way I can populate the comboboxes on my editform.
My follow up question would be this:

I've populated my comboboxes, but the value selected does not get written back to the grid in a normal view (after i've accepted the changes) do i have to do it manually? after the user accepted the changes, do i have to modify the datasource of the grid to reflect the changes?

Thank you
0
Shinu
Top achievements
Rank 2
answered on 09 Aug 2012, 04:18 AM
Hi,

Try the following code snippet to show the GridDropDownColumn value in view mode.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        DataRowView row = (DataRowView)e.Item.DataItem;
        item["ColDType"].Text = row["DataTextField"].ToString(); //For showing the GridDropDownColumn value in view mode.
    }
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editItem = (GridEditableItem)e.Item;
        GridDropDownListColumnEditor dropDownEditor = (GridDropDownListColumnEditor)editItem.EditManager.GetColumnEditor("ColDType"); // columnuniquename
             //your  code
    }
}

Thanks,
Shinu.
0
Andris
Top achievements
Rank 1
answered on 10 Aug 2012, 07:48 PM
This is straight out of your documentation, yet it doesn't work (exception: index out of array bounds)

Protected Sub Products_UpdateCommand(sender As Object, e As GridCommandEventArgs) Handles Products.UpdateCommand
        Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
        Dim ordersTable As DataTable = TryCast(Session("SessionData"), DataTable)
        Dim changedRows As DataRow() = ordersTable.Select("ProductID= " + editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("ProductID").ToString())
        If changedRows.Length <> 1 Then
            MsgBox("Unable to locate the Order for updating.")
            e.Canceled = True
            Return
        End If
        Dim newValues As New Hashtable()
        e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem)
        changedRows(0).BeginEdit()
        Try
            For Each entry As DictionaryEntry In newValues
                changedRows(0)(DirectCast(entry.Key, String)) = entry.Value
            Next
            changedRows(0).EndEdit()
        Catch ex As Exception
            changedRows(0).CancelEdit()
            MsgBox("Unable to update Orders. Reason: " + ex.Message)
            e.Canceled = True
        End Try
    End Sub

exception at this line:

Dim changedRows As DataRow() = ordersTable.Select("ProductID= " + editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("ProductID").ToString())

Here's the full scenario we'd like to be able to do with a $1000 product:

we have a RadGrid with several columns (3 of the dropdowns with options, all of them universal to all rows, meaning i have to have those and exactly those options in their respective dropdownboxes for every row)

we have a DataSource, a DataTable in the variable Session("SessionData").
What we want to do, but seems impossible (and we are getting so nerve-racked about):

have the RadGrid load rows from the session variable. Display each field in its respective column of the row. That we know how to do.

Protected Sub Products_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs) Handles Products.NeedDataSource
       Products.DataSource = TryCast(Session("SessionData"), DataTable)
   End Sub

We have 2 actions for each row: edit and delete. We were able to display those actions on the grid which is great. 
Next: When we click the Edit button the RadGrid changes, it displays editable textboxes and dropdowns each with its respective name next to it. We are able to fill the DropDowns from a datasource. This is great, we are yet again one step closer.

Now when the user clicks the Accept text I assume that we need to update the datasource of the Grid (the Session variable) so when it fires the NeedDataSource event, it reloads into itself the correct values. How do we achieve that? We use the aforementioned UpdateCommand event. Which does not work, even though you yourself redirected us to it. All we did really was change the Datasource and the column name (based on which it should identify the row in the DataSource to update - or are we wrong here?). Yet....it.does.not.work. How come that editedItem.OwnerTableView.DataKeyValues collection is empty?

Let's proceed with the deletion of rows:

When the user clicks the delete button, the DeleteCommand event fires, we identify which row raised the event, identify the row within the DataSource (Session("SessionData")), delete the row so that when the action completes and the Grid rebinds itself (because it should) then it would reload the session variable, but because we deleted the row the user wanted, it would be simply not be loaded into the Grid again.

And well, thats all we really want. Please help us with either explaining or through detailed (working) examples, because we need to account for that $1000 dollars we spent on your product.

So far your support makes it worth it. So far.

Thank you in advance.

0
Shinu
Top achievements
Rank 2
answered on 05 Sep 2012, 06:53 AM
Hi,

Unfortunately I couldn't replicate the issue. I tried the same scenario and its working fine at my end. Here is the sample code I tried.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" onneeddatasource="RadGrid1_NeedDataSource" AutoGenerateColumns="false"  AutoGenerateEditColumn="true" onupdatecommand="RadGrid1_UpdateCommand">
    <MasterTableView DataKeyNames="OrderID">
        <Columns>
            <telerik:GridBoundColumn UniqueName="EmployeeID" DataField="EmployeeID"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="CustomerID" DataField="CustomerID"></telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

VB:
Public Shared connection As String = WebConfigurationManager.ConnectionStrings("NorthwindConnectionString3").ConnectionString
Private conn As New SqlConnection(connection)
Public SqlCommand As New SqlCommand()
 
Protected Sub Page_Load(sender As Object, e As EventArgs)
    If Not IsPostBack Then
        Dim selectQuery1 As String = "select top 10 OrderID,EmployeeID,CustomerID from Orders"
        Dim adapter1 As New SqlDataAdapter(selectQuery1, conn)
        Dim dt1 As New DataTable()
        conn.Open()
        adapter1.Fill(dt1)
        conn.Close()
        Session("SessionData") = dt1
    End If
End Sub
 
Protected Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs)
 
    RadGrid1.DataSource = Session("SessionData")
End Sub
 
Protected Sub RadGrid1_UpdateCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs)
    Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
    Dim ordersTable As DataTable = TryCast(Session("SessionData"), DataTable)
    Dim changedRows As DataRow() = ordersTable.[Select]("OrderID= " + editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("OrderID").ToString())
    If changedRows.Length <> 1 Then
        Interaction.MsgBox("Unable to locate the Order for updating.")
        e.Canceled = True
        Return
    End If
    Dim newValues As New Hashtable()
    e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem)
    changedRows(0).BeginEdit()
    Try
        For Each entry As DictionaryEntry In newValues
            changedRows(0)(DirectCast(entry.Key, String)) = entry.Value
        Next
        changedRows(0).EndEdit()
    Catch ex As Exception
        changedRows(0).CancelEdit()
        Interaction.MsgBox("Unable to update Orders. Reason: " + ex.Message)
        e.Canceled = True
    End Try
End Sub

Thanks,
Shinu.
Tags
Grid
Asked by
Andris
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Andris
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or