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

Bind RadComboBox to RadGrid

5 Answers 139 Views
Grid
This is a migrated thread and some comments may be shown as answers.
RB
Top achievements
Rank 1
RB asked on 24 Feb 2014, 10:31 PM
I am following this example to apply the filtering capability in my RadGrid: http://www.telerik.com/forums/replacing-filtertemplate-on-autogenerated-column#epZqFpXIkk6TVsIFktTiug
Currently my combo.DataSource calls the stored procedure but this is very slow. Is there a way I can populate the RadComboBox from the RadGrid instead of calling the same stored proc again?
This is my RadComboBoxControl_DataBinding

 
public void RadComboBoxControl_DataBinding(object sender, EventArgs e)
  {
      RadComboBox combo = (RadComboBox)sender;
      GridFilteringItem container = (GridFilteringItem)combo.NamingContainer;
 
      combo.DataTextField = colname;
      combo.DataValueField = colname;
      combo.EnableAutomaticLoadOnDemand = true;
      combo.AllowCustomText = true;
      combo.AppendDataBoundItems = true;   
      combo.DataSource = new StoredProc().ExecuteTypedDataTable().SelectDistinct(colname);
      combo.DataBind();
      combo.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem { Text = "All", Value = "All" }); 
      combo.SelectedValue = selectedValue;
  }

5 Answers, 1 is accepted

Sort by
0
Accepted
Eyup
Telerik team
answered on 27 Feb 2014, 10:26 AM
Hi Ruchi,

You can use a global variable to hold the inquired data:
private static DataTable source = null;
protected void Grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    source = GetGridSource();
    (sender as RadGrid).DataSource = source;
}
public class MyCustomTemplate : ITemplate
{
    protected RadComboBox combo;
    public void InstantiateIn(Control container)
    {
        combo = new RadComboBox();
        ...
        combo.DataBinding += combo_DataBinding;
        container.Controls.Add(combo);
    }
    protected void combo_DataBinding(object sender, EventArgs e)
    {
        combo.DataSource = source;
    }
}

In addition, please note that you should avoid calling DataBind() inside databinding related events.

Hope this helps. Please give it a try and let me know if it works for you.

Regards,
Eyup
Telerik
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 UI for ASP.NET AJAX, subscribe to the blog feed now.
0
RB
Top achievements
Rank 1
answered on 04 Mar 2014, 10:21 PM
It works. But I forgot to mention that I need only distinct values. The columns which have the filtering capability will have distinct values but the RadGrid does not have distinct values. Hence, is there a way only distinct values will be populated into the RadComboBox? Is there a way to select only distinct values from each column of the RadGrid? 
0
RB
Top achievements
Rank 1
answered on 04 Mar 2014, 11:29 PM
Ignore it. I found the solution.
0
RB
Top achievements
Rank 1
answered on 04 Mar 2014, 11:48 PM
I am trying to implement the "No Filter" option in the dropdown. If "All" is selected, there should be no filtering and all records should be displayed.  But  it does not work. It returns incorrect data.
void RadComboBoxControl_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        RadComboBox combo = sender as RadComboBox;
        string filterExpression;
    
         filterExpression = "([" + colname + "] ='" + e.Value + "')";
 
        if (e.Value.Equals("All"))
        {
            foreach (GridColumn column in radgrid.MasterTableView.OwnerGrid.Columns)
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
            radgrid.MasterTableView.FilterExpression = string.Empty;
             
        }
        else
         radgrid.MasterTableView.FilterExpression = filterExpression;
        selectedValue = e.Value;
        radgrid.MasterTableView.Rebind();      
    }
}
0
Eyup
Telerik team
answered on 07 Mar 2014, 08:40 AM
Hi Ruchi,

Manipulating the FilterExpression to apply a column filter command can be tricky and complex. I highly suggest that you use the approach demonstrated in this topic to filter with a RadComboBox:
http://demos.telerik.com/aspnet-ajax/grid/examples/functionality/filtering/filter-templates/defaultcs.aspx

If you have difficulties implementing it programmatically, please let me know and I'll gladly help.

Regards,
Eyup
Telerik

DevCraft Q1'14 is here! Join the free online conference to see how this release solves your top-5 .NET challenges. Reserve your seat now!

Tags
Grid
Asked by
RB
Top achievements
Rank 1
Answers by
Eyup
Telerik team
RB
Top achievements
Rank 1
Share this question
or