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

Binding RadComboBox to RadGrid

9 Answers 599 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 14 Nov 2012, 07:49 PM
I am adding a RadComboBox to the EditItemTemplate of a RadGrid.  When I add  SelectedValue='<%# Bind("RegistryValue") %>' to the RadComboBox tag (see first section of code below) I get an "Argument Out Of Range Exception" when I rebind the RadGrid (see second section of code).  If I remove the SelectedValue attribute it works, but the original value is in the RadComboBox when I read it.  I think I am missing something before the RadGrid1.Rebind() command, but I don't know what.  


Adding SelectedValue='<%# Bind("RegistryValue") %>' to the RadComboBox tag caused the problem. 
</telerik:GridTemplateColumn>
      <telerik:GridTemplateColumn DataField="RegistryValue" FilterControlAltText="Filter RegistryValue column" HeaderText="RegistryValue" SortExpression="RegistryValue" UniqueName="RegistryValue">
          <EditItemTemplate>
              <telerik:RadComboBox ID="RadComboBox1" Runat="server" DataSourceID="RegistryValueSqlDataSource" DataTextField="RegValueListValue" DataValueField="RegValueListID" SelectedValue='<%# Bind("RegistryValue") %>'>
              </telerik:RadComboBox>
          </EditItemTemplate>
          <ItemTemplate>
              <asp:Label ID="RegistryValueLabel" runat="server" Text='<%# Eval("RegistryValue") %>'></asp:Label>
          </ItemTemplate>
      </telerik:GridTemplateColumn>


 When RadGrid1.Rebind() is called I get an Argument Out Of Range Exception error. 

protected
void EditRadButton_Click(object sender, EventArgs e)
  {
      foreach (GridItem item in RadGrid1.MasterTableView.Items)
      {
          if (item is GridEditableItem)
          {
              GridEditableItem editableItem = item as GridDataItem;
              editableItem.Edit = true;
          }
      }
      RadGrid1.Rebind();
  }

9 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 15 Nov 2012, 08:37 AM
Hi Scott,

The error occured because you have set two different datafield as DataValueField and SelectedValue. I suppose you want to show another field as selected other than data in DataValueField and DataTextField. Please take a look into the following code snippet.

ASPX:
<telerik:GridTemplateColumn DataField="RegistryValue" FilterControlAltText="Filter RegistryValue column" HeaderText="RegistryValue" SortExpression="RegistryValue" UniqueName="RegistryValue">
          <EditItemTemplate>
              <telerik:RadComboBox ID="RadComboBox1" Runat="server" DataSourceID="RegistryValueSqlDataSource" DataTextField="RegValueListValue" DataValueField="RegValueListID" >
              </telerik:RadComboBox>
          </EditItemTemplate>
          <ItemTemplate>
              <asp:Label ID="RegistryValueLabel" runat="server" Text='<%# Eval("RegistryValue") %>'></asp:Label>
          </ItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void dgStations_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editItem = (GridEditableItem)e.Item;
        RadComboBox combo = (RadComboBox)editItem.FindControl("RadComboBox1");
        combo.SelectedItem.Text = (string)DataBinder.Eval(e.Item.DataItem, "RegistryValue").ToString();      
    }      
}

Thanks,
Princy.
0
Scott
Top achievements
Rank 1
answered on 15 Nov 2012, 08:37 PM
Princy,

This seems to be better, but what does dgStations_ItemDataBound get called from?  I have tried RadGrid1_ItemDataBound and RadComboBox1_ItemDataBound, but neither are called when I change the value of RadComboBox1.  When I click on a different row, cell, or save, the combo box goes back to its original value.  

Scott    
0
Princy
Top achievements
Rank 2
answered on 16 Nov 2012, 04:45 AM
Hi Scott,

I didn't get your requirement. The ItemDataBound event occurs when a data item is bound to data in a Telerik RadGrid control. In the above case it is called when the edit button is clicked. The event called on Changing the value of Combobox is SelectedIndexChanged event of RadCombobox.

Thanks,
Princy.
0
Scott
Top achievements
Rank 1
answered on 16 Nov 2012, 05:08 PM
I am using an edit button that is outdide of the RadGrid control, but I don't think that matters.  When I click my edit button protected void EditRadButton_Click(object sender, EventArgs e) is called and also protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e).  The problem is when I change the value of the RadComboBox it is not called.  I added  protected void RadComboBox1_ItemDataBound(object sender, RadComboBoxItemEventArgs e) which is called when the combo box is changed, but I don't know how to adapt the code you gave me to work with protected void RadComboBox1_ItemDataBound(object sender, RadComboBoxItemEventArgs e).

0
Elliott
Top achievements
Rank 2
answered on 16 Nov 2012, 05:46 PM
the combo box goes back to its original value because you have not updated the original data source
do you want to permanently change the value in the field shown in the combo box?  then do it
otherwise, it will revert to what it had been - this is expected behavior

as far as I know, a combo box does not have a ItemDataBound event
- and if it does triggering it does not fire the grid itemdatabound event
0
Scott
Top achievements
Rank 1
answered on 16 Nov 2012, 07:37 PM
When I change the combo box and then click on any other area the combo box goes back th the original value.  I am using  EditMode="InPlace" if that matters.  The only thing that is called when I change the dropdown is RadComboBox1_ItemDataBound.  I don't know how to change the code I was given to work with RadComboBox1_ItemDataBound(object sender, RadComboBoxItemEventArgs e). I assume that this will fix the problem.  I also removed SelectedValue='<%# Bind("RegistryValue") %>' from my original because I thought that the ItemDataBound procedure would replace that. 

The code below has problems with:
  e.Item is GridEditableItem
   e.Item.IsInEditMode
   (GridEditableItem)e.Item;
protected void RadComboBox1_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
     {
         if (e.Item is GridEditableItem && e.Item.IsInEditMode)
         {
             GridEditableItem editItem = (GridEditableItem)e.Item;
             RadComboBox combo = (RadComboBox)editItem.FindControl("RadComboBox1");
             combo.SelectedItem.Text = (string)DataBinder.Eval(e.Item.DataItem, "RegistryValue").ToString();
         }
    }
0
Elliott
Top achievements
Rank 2
answered on 16 Nov 2012, 07:54 PM
I don't do a lot of client-side binding

protected void RadComboBox1_ItemDataBound etc

what event is this handling?
is e.Item the row in the grid?
I would have thought sender.NamingContainer is the row in the grid
if you look at Princy's code, the event handler is on the grid, not the combo box

just a thought - implement the SelectedValueChanged (if that doesn't work, the SelectedIndexChanged) event handler of the ComboBox
attach it to the combo box using Princy's logic

0
Scott
Top achievements
Rank 1
answered on 16 Nov 2012, 09:08 PM
Marianne,

Thank you for your help.  I am not sure how to answer some of your questions.  I am trying to add a RadComboBox to a RadGrid.  I am using InPlace editing.  Without the RadComboBox everything works.  When I add the RadComboBox the selected value is not saved.  I would normally use Two-way databinding on the RadComboBox in the EditItemTemplate of the RadGrid, but when I do this the program crashes when I rebind the RadGrid.  This is shown in my original post.   

Princy took out this data binding and had me do it with dgStations_ItemDataBound.  I am not sure what called this, but I used RadGrid1_ItemDataBound with his code.  This was called when I edited the RadGrid, but not when I changed the RadComboBox.  Whenever I click on some other cell or save the RadGrid, RadComboBox goes back to the original value.  I can see this on the page before any break point is called in the code.  The only way I have been able to catch this action before it reverts back to the default is to use RadComboBox1_ItemDataBound.  My problem here is a lack of understanding object sender and RadComboBoxItemEventArgs e.  The same code that works for (object sender, GridItemEventArgs e) in the RadGrid ItemDataBound does not work for RadComboBox ItemDataBound.   It might simply be changing the code to work with RadComboBoxItemEventArgs instead of GridItemEventArgs.  I have tried changing this, but have not gotten it to work.    

I would prefer to fix the SelectedValue='<%# Bind("RegistryValue") %>' failure, but I don't understand why it is failing.  Could it be a problem with what is stored in the RadComboBox and what the RadGrid is expecting?  Both are populated from a database that stores varchar(50).  

The other thought was that I was missing something in the EditRadButton_Click function.   

Thanks,
Scott
 
0
Elliott
Top achievements
Rank 2
answered on 16 Nov 2012, 09:43 PM
I have been posting because I needed to do something like what you're doing
I looked at my kode and realized - I used a GridDropDownColumn instead of templating with a RadComboBox
the only time I use a RadComboBox is in a UserControl
I've left out some columns

<telerik:RadGrid ID="gvVendor" runat="server" GridLines="None">
<MasterTableView DataKeyNames="VendorID" AutoGenerateColumns="false" AllowSorting="true" AllowPaging="true" CommandItemDisplay="Top" EditMode="InPlace" >
<Columns>
    <telerik:GridBoundColumn UniqueName="VendorID" DataField="VendorID" HeaderText="VendorID" ReadOnly="true" />
    <telerik:GridTemplateColumn UniqueName="VendorName" HeaderText="Vendor Name" HeaderStyle-Width="200px" >
    <ItemTemplate>
        <asp:Label ID="lblVendorName" Text='<%# Bind("VendorName") %>' runat="server" />
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadTextBox ID="rtbVendorName" Text='<%# Eval("VendorName") %>' runat="server" />
        <asp:RequiredFieldValidator ID="rfvVendorName" ControlToValidate="rtbVendorName" ErrorMessage="*" runat="server" />
    </EditItemTemplate>
    </telerik:GridTemplateColumn>
    <telerik:GridCheckBoxColumn UniqueName="RepGroupFlag" HeaderText="Rep Group?" />
    <telerik:GridDropDownColumn UniqueName="RepGroup" DropDownControlType="RadComboBox" EnableEmptyListItem="True" EmptyListItemValue="0" EmptyListItemText="Select One" ListValueField="RepGroupID" ListTextField="RepGroups" HeaderText="Rep Group" />
    <telerik:GridEditCommandColumn EditText="Edit" />                       
    <telerik:GridButtonColumn UniqueName="DeleteColumn" CommandName="Delete" Text="Delete" />
</Columns>
</MasterTableView>
</telerik:RadGrid>
Private Sub GetAllVendors()
    Dim ds As DataSet = Nothing
    Dim dt As DataTable = Nothing
    Dim ws As New CommonFunctions
    ds = ws.GetAllVendors()
    If ds.Tables.Count > 0 Then
        dt = ds.Tables(0)
    End If
    gvVendor.DataSource = dt
End Sub
 
Protected Sub gvVendor_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles gvVendor.NeedDataSource
    GetAllVendors()
End Sub
 
Protected Sub gvVendor_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles gvVendor.ItemDataBound
    Dim dgItem As GridDataItem = Nothing
    Dim gefItem As GridEditFormItem = Nothing
    Dim geItem As GridEditableItem = Nothing
 
    If TypeOf e.Item Is IGridInsertItem Then
    ElseIf TypeOf e.Item Is GridDataItem Then
        dgItem = CType(e.Item, GridDataItem)
        FillInCheckBoxes(dgItem)
    End If
    If TypeOf e.Item Is GridEditableItem Then
        geItem = CType(e.Item, GridEditableItem)
        FillInRepGroups(geItem)
    End If
End Sub
 
Private Sub FillInRepGroups(ByVal geItem As GridEditableItem)
    Dim geManager As GridEditManager = Nothing
    Dim gddlEditor As GridDropDownListColumnEditor = Nothing
    Dim rcbRepGroup As RadComboBox
    Dim rcbItem As RadComboBoxItem = Nothing
    Dim ds As DataSet = Nothing
    Dim ws As CommonFunctions
 
    If geItem.IsInEditMode Then
    Else
        Exit Sub
    End If
    geManager = geItem.EditManager
    gddlEditor = CType(geManager.GetColumnEditor("RepGroup"), GridDropDownColumnEditor)
    rcbRepGroup = gddlEditor.ComboBoxControl
    ws = New CommonFunctions
    ds = ws.GetRepGroups
    rcbRepGroup.DataSource = ds.Tables(0)
    rcbRepGroup.DataValueField = "RepGroupsID"
    rcbRepGroup.DataTextField = "RepGroups"
    rcbRepGroup.DataBind()
    rcbItem = New RadComboBoxItem("Select One", 0)
    rcbRepGroup.Items.Insert(0, rcbItem)
End Sub
 
Private Sub FillInCheckBoxes(ByVal gdItem As GridDataItem)
    Dim chkRepGroup, chkRegistration As CheckBox
    Dim litRepGroup As Literal = Nothing
    Dim drView As DataRowView
 
    more kode
 
    If TypeOf gdItem("RepGroup").Controls(0) Is Literal Then
        litRepGroup = CType(gdItem("RepGroup").Controls(0), Literal)
        litRepGroup.Text = drView("RepGroup").ToString
    End If
End Sub


a bit of kode (and VB.NET!) but it does not "lose" the selected value
Tags
Grid
Asked by
Scott
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Scott
Top achievements
Rank 1
Elliott
Top achievements
Rank 2
Share this question
or