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

Apply RowFilter to GridView

3 Answers 96 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lukas
Top achievements
Rank 1
Lukas asked on 10 Jan 2014, 01:32 PM
Hello Forum,

My Name is Lukas, I am new to the Telerik Controls. First i want to say that these controls helped me a lot an you musst keep up your nice work!

So now to my Problem:

I got a Grid on my aspx.net Site filled with an EntityDataSource. The Data for this comes from an SQL-Server. On the Top of this Site there is an DropDownList filled with some example companys.

So when i change the company of the DropDownList i want to apply a Filter to the "CompanyID"-Row in the Grid-View with the Index of the selected Item in the DropDownList.

I got the Event from the DropDownList and the only thing i want to know is how i can filter the "CompanyID"-Row from the Grid to the selected Index of the DropDownList. So that the Grid only shows the data where "CompanyID" is for example 4.

Is this possibly and can you give me an example how to do that? I searched a lot in this forum but haven´t found an simple answer for this case.

Kind regards
Lukas

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 13 Jan 2014, 05:26 AM
Hi Lukas,

Please try changing the DataSource of the RadGrid in the OnSelectedIndexChanged of the DropDownList as shown below.

ASPX:
<asp:DropDownList ID="ddlFilter" runat="server" DataSourceID="DataSource2" DataTextField="OrderID"
    DataValueField="OrderID" AutoPostBack="true" OnSelectedIndexChanged="ddlFilter_SelectedIndexChanged">
</asp:DropDownList>
<telerik:RadGrid ID="RadGrid1" runat="server"  AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="OrderID">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void ddlFilter_SelectedIndexChanged(object sender, EventArgs e)
{
        DropDownList ddlFilter=(DropDownList)sender;
        string selectedValue = ddlFilter.SelectedValue.ToString();
        // query to get the datasource
        RadGrid1.DataSource =//your datasource;
        RadGrid1.DataBind();
}
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
  //populate the radgrid here
}

Thanks,
Princy.


0
Lukas
Top achievements
Rank 1
answered on 13 Jan 2014, 08:59 AM
Hi Princy,

I have implemented your code but there seems to be an issue. When the page load with the Grid, the grid shows all the Data unfiltered. The Data is displayed over 2 pages and one page shows 10 Items.

When i now change the DropDownList and filter the Grid like you in your example, the Grid shows the right Items. But the Grid also says that there are 2 Pages and 11 Items. With the filtered DataSource there are only 7. The unfiltered DataSource got 11 Items.

And what do you mean by populate the radgrid? The NeedDataSource-Event is called, when i want to Insert a new Record using the CommandItem. When i use this Function, the GridView is empty and i can´t insert a new Record or modify the existing.

What shoul i do in this Event? Apply the filtered DataSource again to the GridView?

Kind regards

Lukas

0
Lukas
Top achievements
Rank 1
answered on 13 Jan 2014, 01:29 PM
Hi,

I found an Solution that worked perfekt for me.

Grid1.MasterTableView.FilterExpression = "it.CompanyID = " + e.Index;
Grid1.MasterTableView.Rebind();

Thanks for your help! 

Kind Regards

Lukas
Tags
Grid
Asked by
Lukas
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Lukas
Top achievements
Rank 1
Share this question
or