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

How to get FilterFunction & FilterValue from code behind

11 Answers 1282 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vatsal
Top achievements
Rank 2
Vatsal asked on 28 May 2012, 01:45 PM
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,

11 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 28 May 2012, 02:17 PM
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.
0
Vatsal
Top achievements
Rank 2
answered on 29 May 2012, 11:30 AM
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
0
Shinu
Top achievements
Rank 1
answered on 29 May 2012, 12:01 PM
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.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 29 May 2012, 12:05 PM
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
0
Vatsal
Top achievements
Rank 2
answered on 29 May 2012, 01:16 PM
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,
0
Vatsal
Top achievements
Rank 2
answered on 29 May 2012, 01:29 PM
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... :)
0
CQT
Top achievements
Rank 1
answered on 18 Jul 2012, 01:32 PM
Hello, I have the same problem as yours... did you found any solution?
0
Vatsal
Top achievements
Rank 2
answered on 19 Jul 2012, 10:37 AM
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 
0
CQT
Top achievements
Rank 1
answered on 23 Jul 2012, 12:35 PM

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?

0
Chinh
Top achievements
Rank 1
answered on 01 Oct 2014, 07:15 AM
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.
0
Viktor Tachev
Telerik team
answered on 03 Oct 2014, 02:49 PM
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.

 
Tags
Grid
Asked by
Vatsal
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 1
Vatsal
Top achievements
Rank 2
Jayesh Goyani
Top achievements
Rank 2
CQT
Top achievements
Rank 1
Chinh
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or