How to get FilterFunction & FilterValue from code behind

12 posts, 0 answers
  1. Vatsal
    Vatsal avatar
    26 posts
    Member since:
    Oct 2010

    Posted 28 May 2012 Link to this post

    Hi,

    I am using below code to find out values of filtered column's "filter value" and "function name" but it's not working in my page.

    foreach (GridColumn item in RadGrid1.MasterTableView.Columns)
    {
            string filterFunction = item.CurrentFilterFunction.ToString();
            string filterValue = item.CurrentFilterValue;
    }

    I want to save these two values in DB for future use. Please help me...

    Thanks,
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 28 May 2012 Link to this post

    Hello Vatsal,

    Try the following code in ItemCommand to access filter function from code.
    C#:
    void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
     if (e.CommandName == RadGrid.FilterCommandName)
     {
       Pair filterPair = (Pair)e.CommandArgument;
       string value = filterPair.First.ToString();//accessing function name
       foreach (GridColumn col in RadGrid1.Columns)
       {
         string filter=  RadGrid1.MasterTableView.GetColumn(col.UniqueName).CurrentFilterValue.ToString();
        }
      }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Vatsal
    Vatsal avatar
    26 posts
    Member since:
    Oct 2010

    Posted 29 May 2012 Link to this post

    Hi,

    Thanks for your reply, but I want to run this code out side grid's event. i.e. on click event of button which is outside of grid and it's not working I checked it in my project.

    protected void btn_Click(object sender, EventArgs e)
    {
            foreach (GridColumn item in RadGrid1.MasterTableView.Columns)
            {
                     string filterFunction = RadGrid1.MasterTableView.GetColumn(item.UniqueName).CurrentFilterFunction.ToString();
                     string filterValue = RadGrid1.MasterTableView.GetColumn(item.UniqueName).CurrentFilterValue;
            }
    }

    it always gives me filterFunction = "No Filter" and filterValue = "" even I have applied filters before click on button.

    Please help me

    Thanks
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 29 May 2012 Link to this post

    Hi Vatsal,

    I suppose the issue arises while iterating the columns on filtering. I have tried the same scenario and the following code worked as expected.
    C#:
    public static string filter =string.Empty;
    void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
      if (e.CommandName == RadGrid.FilterCommandName)
      {
       Pair filterPair = (Pair)e.CommandArgument;
       filter = filterPair.Second.ToString();
      }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
     foreach (GridColumn item in RadGrid1.MasterTableView.Columns)
     {
        string filterFunction = RadGrid1.MasterTableView.GetColumn(filter).CurrentFilterFunction.ToString();
        string filterValue = RadGrid1.MasterTableView.GetColumn(filter).CurrentFilterValue;
     }
    }

    Thanks,
    Shinu.
  6. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 29 May 2012 Link to this post

    Hello Vatsal,

    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
          <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
           AllowFilteringByColumn="true">
             
              <MasterTableView CommandItemDisplay="Top">
                
                  <Columns>
                      <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID" Aggregate="Count">
                      </telerik:GridBoundColumn>
                  </Columns>
              </MasterTableView>
          </telerik:RadGrid>
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            DateTime dt = DateTime.Now;
     
      dynamic data = new[] {
                    new { ID = 1, Name ="Name_1"},
                    new { ID = 2, Name = "Name2"},
                    new { ID = 3, Name = "Name3"},
                    new { ID = 4, Name = "Name4"},
                    new { ID = 5, Name = "Name5"}
                };
     
     
            RadGrid1.DataSource = data;
        }
     
    protected void Button1_Click(object sender, EventArgs e)
        {
            string str1 = RadGrid1.MasterTableView.Columns.FindByUniqueName("ID").CurrentFilterFunction.ToString();
            string str2 = RadGrid1.MasterTableView.Columns.FindByUniqueName("ID").CurrentFilterValue;
     
        }

    First filtering the ID column in RadGrid then after click on button And you can get value of current filterfunction and filtervalue.

    let me know if any concern.

    Thanks,
    Jayesh Goyani
  7. Vatsal
    Vatsal avatar
    26 posts
    Member since:
    Oct 2010

    Posted 29 May 2012 Link to this post

    Hi,

    Thanks a lot to both ( Shinu & Jayesh )

    I want to mention one important point here is...

    I am creating grid columns dynamically based on data table I received from data layer. Is that problem I am not getting values in "CurrentFilterFunction" and "CurrentFilterValue" properties? Because using same code above,  I am still not getting correct filter function and value.

    I am adding GridBoundColumn dynamically from code before binding grid. However, I can see filter expression using below code line in same button's click event.

    string filterExpression = RadGrid1.MasterTableView.FilterExpression;

    but not able to get individual values of function name and value from column.

    Sorry I forgot to mention functionality of dynamically creating grid columns.

    Thanks,
  8. Vatsal
    Vatsal avatar
    26 posts
    Member since:
    Oct 2010

    Posted 29 May 2012 Link to this post

    Yes!!! that's the problem I found it. Because I clear all columns and re-create all each time needDataSoruce fires, the filtered values lost after rebind.

    I am working on it and will let you know if any problem.

    Thanks again Shinu & Jayesh... :)
  9. CQT
    CQT avatar
    21 posts
    Member since:
    May 2011

    Posted 18 Jul 2012 Link to this post

    Hello, I have the same problem as yours... did you found any solution?
  10. Vatsal
    Vatsal avatar
    26 posts
    Member since:
    Oct 2010

    Posted 19 Jul 2012 Link to this post

    Hi,

    If you are creating grid's columns dynamically from code behind, you need to maintain each Pair of filter and filter value in viewstate and need to apply these pairs and value again on rebind after creating columns. Please see below:

    public Dictionary<string, Pair> Filters
    {
        get { return (Dictionary<string, Pair>)ViewState["gridFilters"]; }
        set { ViewState["gridFilters"] = value; }
    }
     
    protected void radGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
    if (e.CommandName == RadGrid1.FilterCommandName)
                    {
                        Pair filter = (Pair)e.CommandArgument;
                        Filters.Add(radGridReportData.MasterTableView.Columns.FindByUniqueName(filter.Second.ToString()).CurrentFilterValue, filter);
                    }
    }
     
    protected void BindGrid()
    {
         // Add dynamic columns to grid first...
     
        foreach (KeyValuePair<string, Pair> filter in Filters)
                    {
                        GridColumn col = radGrid1.MasterTableView.Columns.FindByUniqueNameSafe(filter.Value.Second.ToString());
                        switch (filter.Value.First.ToString())
                        {
                            case "NoFilter":
                                col.CurrentFilterFunction = GridKnownFunction.NoFilter;
                                col.CurrentFilterValue = "";
                                break;
                            case "Contains":
                                col.CurrentFilterFunction = GridKnownFunction.Contains;
                                col.CurrentFilterValue = filter.Key;
                                break;
                            case "DoesNotContain":
                                col.CurrentFilterFunction = GridKnownFunction.DoesNotContain;
                                col.CurrentFilterValue = filter.Key;
                                break;
                            case "EndsWith":
                                col.CurrentFilterFunction = GridKnownFunction.EndsWith;
                                col.CurrentFilterValue = filter.Key;
                                break;
                            case "EqualTo":
                                col.CurrentFilterFunction = GridKnownFunction.EqualTo;
                                col.CurrentFilterValue = filter.Key;
                                break;
                            case "GreaterThan":
                                col.CurrentFilterFunction = GridKnownFunction.GreaterThan;
                                col.CurrentFilterValue = filter.Key;
                                break;
                            case "GreaterThanOrEqualTo":
                                col.CurrentFilterFunction = GridKnownFunction.GreaterThanOrEqualTo;
                                col.CurrentFilterValue = filter.Key;
                                break;
                            case "LessThan":
                                col.CurrentFilterFunction = GridKnownFunction.LessThan;
                                col.CurrentFilterValue = filter.Key;
                                break;
                            case "LessThanOrEqualTo":
                                col.CurrentFilterFunction = GridKnownFunction.LessThanOrEqualTo;
                                col.CurrentFilterValue = filter.Key;
                                break;
                            case "NotEqualTo":
                                col.CurrentFilterFunction = GridKnownFunction.NotEqualTo;
                                col.CurrentFilterValue = filter.Key;
                                break;
                            case "StartsWith":
                                col.CurrentFilterFunction = GridKnownFunction.StartsWith;
                                col.CurrentFilterValue = filter.Key;
                                break;
                        }
                    }
        // Set grid datasource here...
    }

    Let me know if you need more help...

    Thanks,
    Vatsal 
  11. CQT
    CQT avatar
    21 posts
    Member since:
    May 2011

    Posted 23 Jul 2012 Link to this post

    Many Thanks for your help, I used your code in my application but I'm not able to get it functioning... I always get a System.NullReferenceException in this point of the code : foreach (KeyValuePair<string, Pair> filter in Filters)

    Could you please give me the whole source code of your page to try to understand when my code fails?

  12. Chinh
    Chinh avatar
    1 posts
    Member since:
    Jun 2014

    Posted 01 Oct 2014 Link to this post

    hi everyone in Telerik team,
    I have a error  follow : in the radgrid, Filter values is '*' lose, I dont know.
    example : I have a filter value : PT12*1 => it display with PT12, the char  : * and after that are no display.
    Thank you.
  13. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 03 Oct 2014 Link to this post

    Hello Chinh,

    Modifying the FilterExpression for RadGrid could be done in the code-behind. Check out the following articles that describe in more detail how this functionality can be used.

    Regards,
    Viktor Tachev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017