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

Custom Filtering then radgrid filter

3 Answers 61 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dao
Top achievements
Rank 1
Dao asked on 11 Sep 2012, 12:36 AM
here my code:
KTFrameworkCusEntities dcc = new KTFrameworkCusEntities();
IQueryable<
udv_Articles> _udvResult;
    public IEnumerable<udv_Articles> getAllArticleByCat(int catid)
    {
  
        //KTFrameworkCusEntities dc = new KTFrameworkCusEntities();
        //_udvPro = null;
        _udvResult = dcc.udv_Articles.Include("udv_Categories").Where(t => t.ArticleID == 99999999);
        loadArticlebyCate(catid);
        return _udvResult.OrderByDescending(t => t.UpdatedDate).ToList();
    }
  
    protected IQueryable<udv_Articles> loadArticlebyCate(int caid)
    {
        //KTFrameworkCusEntities dc = new KTFrameworkCusEntities();
  
        Func<KTFrameworkCusEntities, IQueryable<udv_Articles>> _IEProList = CompiledQuery.Compile((KTFrameworkCusEntities ne) => ne.udv_Articles.Include("udv_Categories").Where(t => t.udv_Categories.CateID == caid));
  
        if (_IEProList(dcc).Count() > 0)
        {
            _udvResult = _udvResult.Concat(_IEProList(dcc));
        }
        IEnumerable<udv_Categories> _IECatList = dcc.udv_Categories.Where(c => c.ParentID == caid);
        if (_IECatList.Count() > 0)
        {
            foreach (var item in _IECatList)
            {
                loadArticlebyCate(item.CateID);
            }
        }
  
        return _udvResult;
    }
 
IQueryable<udv_Categories> _udvResult2;
    public IEnumerable<udv_Categories> getAllCategoriesByCat(int catid)
    {
        //KTFrameworkCusEntities dc = new KTFrameworkCusEntities();
        //_udvPro = null;
        _udvResult2 = dcc.udv_Categories.Where(t => t.CateID == 99999999);
        loadCategorybyCate(catid);
        return _udvResult2.Where(t => t.IsActive == true).OrderBy(t => t.DisplayOrder).ToList();
    }
 
    protected IQueryable<udv_Categories> loadCategorybyCate(int caid)
    {
        //KTFrameworkCusEntities dc = new KTFrameworkCusEntities();
 
        Func<KTFrameworkCusEntities, IQueryable<udv_Categories>> _IEProList = CompiledQuery.Compile((KTFrameworkCusEntities ne) => ne.udv_Categories.Where(t => t.ParentID == caid));
 
        //IEnumerable<udv_Categories> _IEProList = dc.udv_Categories.Where(t => t.ParentID == caid);
        //IEnumerable<udv_Articles> _IEProList = from c in dc.udv_Articles where c.udv_Categories.CateID == caid select c;
        if (_IEProList(dcc).Count() > 0)
        {
            _udvResult2 = _udvResult2.Concat(_IEProList(dcc));
        }
        IEnumerable<udv_Categories> _IECatList = dcc.udv_Categories.Where(c => c.ParentID == caid);
        if (_IECatList.Count() > 0)
        {
            foreach (var item in _IECatList)
            {
                loadCategorybyCate(item.CateID);
            }
        }
        return _udvResult2;
 
    }

<telerik:RadGrid ID="RadGrid1" AllowFilteringByColumn="true" PageSize="20" ShowFooter="True"
    AllowPaging="True" AutoGenerateColumns="false" GridLines="None" ShowStatusBar="true"
    runat="server" OnItemDataBound="RadGrid1_ItemDataBound">
    <GroupingSettings CaseSensitive="false" />
    <MasterTableView EditMode="InPlace" AllowFilteringByColumn="True" ShowFooter="True"
        DataKeyNames="ArticleID" TableLayout="Fixed">
        <Columns>
            <telerik:GridBoundColumn DataField="Title" HeaderText="Tiêu đề" AutoPostBackOnFilter="true"
                CurrentFilterFunction="Contains" ShowFilterIcon="false">
                <HeaderStyle Width="25%" />
            </telerik:GridBoundColumn>
 
           <telerik:GridTemplateColumn DataField="CateID" HeaderText="Danh mục" UniqueName="CateID"
                    HeaderStyle-Width="200px" SortExpression="DisplayOrder">
                    <FilterTemplate>
                        <telerik:RadComboBox ID="RadComboBoxCategories" DataTextField="Title"
                        
                            DataValueField="CateID" Height="100px" AppendDataBoundItems="true"
                            runat="server" >
                            <Items>
                                <telerik:RadComboBoxItem Text="All" />
                            </Items>
                        </telerik:RadComboBox>                          
                    </FilterTemplate>
                    <ItemTemplate>
                       <%# Eval("udv_Categories.Title") %>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

if(!ispostback){
  RadGrid1.DataSource = getAllArticleByCat(113);
                RadGrid1.DataBind();
}
 
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridFilteringItem)
        {
            GridFilteringItem item = (GridFilteringItem)e.Item;
            RadComboBox combo = (RadComboBox)item.FindControl("RadComboBoxCategories");
            using (dmsFunction dmsf = new dmsFunction())
            {
                combo.DataSource = getCategoryByParent(113).ToList();
                combo.DataBind();
                combo.AutoPostBack = true;
                combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged);
            }
        }
    }
 
    void combo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
       // Custom Filter: Call getAllArticleByCat(Convert.ToInt32(combo.SelectedValue)) to get all Article by Categories
       // Then RadGrid Filter
          // Teach me how... Thanks
    }

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 11 Sep 2012, 04:45 AM
Hi Dao,

Try the following code snippet to filter RadComboBox.

C#:
void combo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    string filterExpression;
    filterExpression = "([CateID] = '" + e.Value + "')";
    RadGrid1.MasterTableView.FilterExpression = filterExpression;
    RadGrid1.MasterTableView.Rebind();
}

Thanks,
Shinu.
0
Dao
Top achievements
Rank 1
answered on 11 Sep 2012, 11:45 AM
Not working :(
0
Shinu
Top achievements
Rank 2
answered on 12 Sep 2012, 03:15 AM
Hi,

Here is the sample code snippet I tried to do the filter using RadComboBox.

ASPX:
<telerik:GridTemplateColumn DataField="OrderID"  HeaderText="Danh mục" UniqueName="CateID" HeaderStyle-Width="200px" SortExpression="OrderID">
        <FilterTemplate>
            <telerik:RadComboBox ID="RadComboBoxCategories" AppendDataBoundItems="true" runat="server">
                <Items>
                    <telerik:RadComboBoxItem Text="All" />
                </Items>
            </telerik:RadComboBox>                         
        </FilterTemplate>
        <ItemTemplate>
            <%# Eval("OrderID") %>
        </ItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridFilteringItem)
    {
        GridFilteringItem item = (GridFilteringItem)e.Item;
        RadComboBox combo = (RadComboBox)item.FindControl("RadComboBoxCategories");
        combo.DataSource = DataSource1;
        combo.DataTextField = "OrderID";
        combo.DataValueField = "OrderID";
        combo.DataBind();
        combo.AutoPostBack = true;
        combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged);
    }
}
void combo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    string filterExpression;
    filterExpression = "([OrderID] = '" + e.Value + "')";
    RadGrid1.MasterTableView.FilterExpression = filterExpression;
    RadGrid1.MasterTableView.Rebind();
}

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