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

How to use a combobox when editing a cell in a grid

6 Answers 589 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marc Fearby
Top achievements
Rank 1
Marc Fearby asked on 12 Apr 2012, 06:57 AM
How can I customise the inplace or form-based editor for a field so that it shows a combobox with records from a datasource? I've tried editing the grid template and putting a combo+datasource in various places but all I get is basic text boxes.

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 12 Apr 2012, 07:29 AM
Hello Marc,

You can either use EditItemTemplate or a GridDropDownColumn( displays a drop-down control for each edited cell in the column) for populating the combobox in edit mode. Here is the sample code.
aspx:
<telerik:GridTemplateColumn>
  <EditItemTemplate>
    <telerik:RadComboBox ID="RadComboBox1" runat="server">
  </EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridDropDownColumn UniqueName="DropDown"  HeaderText="dropdown"  DropDownControlType="RadComboBox"></telerik:GridDropDownColumn>
C#:
//Populating RdaComboBox
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridEditableItem && e.Item.IsInEditMode)
 {
   GridEditableItem item = (GridEditableItem)e.Item;
   RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1");
   combo.DataSourceID = "SqlDataSource1";
   combo.DataTextField="EmployeeID";
   combo.DataValueField="EmployeeID";
  }
}
 //Populating GridDropDownColumn
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
 if (e.Item is GridEditableItem && e.Item.IsInEditMode)
 {
   GridEditableItem item = (GridEditableItem)e.Item;
   RadComboBox RadComboBox1 = (RadComboBox)item["DropDown"].Controls[0];
   RadComboBox1.DataSourceID = "SqlDataSource2";
   RadComboBox1.DataTextField = "FirstName";
   RadComboBox1.DataValueField = "FirstName";
   RadComboBox1.DataBind();
 }
}

Thanks,
Shinu.
0
Marc Fearby
Top achievements
Rank 1
answered on 13 Apr 2012, 02:08 AM
Thanks. I can now see a combo with records. However, when trying to get the values from the insert form, it's showing my two ordinary textboxes from the GridBoundColumns but not the value from my new GridTemplateColumn:

Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
        Dim newValues As Hashtable = New Hashtable
        ' Get the names/values from the new item and store in a new dictionary (hashtable) object
        e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem)
        For Each entry As DictionaryEntry In newValues
            'newRow(CType(entry.Key, String)) = entry.Value
        Next

I copied (and trimmed) the above code from here. I don't use ASP.NET normally (except for WCF web services) so I may be asking something obvious, for which I apologise :-) I just need to make a few changes to a codebase I inherited.
Thanks.
0
Marc Fearby
Top achievements
Rank 1
answered on 13 Apr 2012, 02:20 AM
Disregard. I switched to GridDropDownColumn method and it's appearing in the hashtable.
0
Marc Fearby
Top achievements
Rank 1
answered on 13 Apr 2012, 02:41 AM
Actually, I think I'm still going to need to know how to retrieve the value out of an ordinary combo box because I also need a combo with auto-complete functionality (courtesy of OnClientItemsRequesting and OnItemsRequested events, which I'm not sure you can add to a GridDropDownColumn?)
0
Shinu
Top achievements
Rank 2
answered on 13 Apr 2012, 08:09 AM
Hello Marc,

GridDropDownColumn supports automatic load-on-demand for the RadComboBox editor. The column provides the propertyAllowAutomaticLoadOnDemand indicating whether automatic load-on-demand is enabled for the RadComboBox. Load-on-demand for a DropDownColumn works only with declarative data source controls set through the DataSourceID property of the column. Using DataSets and binding the column through its DataMember property is not compatible with automatic load-on-demand.You can access the column in ItemDataBound event and attach the event as shown below.
aspx:
<telerik:GridDropDownColumn UniqueName="dropdown"  AllowAutomaticLoadOnDemand="true" ListTextField="FirstName" ListValueField="FirstName" DataSourceID="SqlDataSource1" DataField="FirstName" DropDownControlType="RadComboBox"></telerik:GridDropDownColumn>
C#:
protected void grid1_ItemCreated(object sender, GridItemEventArgs e)
{
  if (e.Item is GridEditableItem && e.Item.IsInEditMode)
  {
   GridEditableItem item = (GridEditableItem)e.Item;
   RadComboBox RadComboBox1 = (RadComboBox)item["dropdown"].Controls[0];
   RadComboBox1.ItemsRequested += new RadComboBoxItemsRequestedEventHandler(RadComboBox1_ItemsRequested);
  }
}
void RadComboBox1_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
      
}

Thanks,
Shinu.
0
Marc Fearby
Top achievements
Rank 1
answered on 16 Apr 2012, 05:41 AM
Thanks for your help. I have been reminded why I didn't choose ASP.NET for my own projects :-)
Tags
Grid
Asked by
Marc Fearby
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Marc Fearby
Top achievements
Rank 1
Share this question
or