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

Correct approach to custom filtering in code?

1 Answer 88 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 1
Rob asked on 21 Jan 2009, 12:29 PM
Can you suggest the best approach to filtering a grid's data in code rather than by using the built-in filtering functionality?

Once the grid has been rendered, in the click event of a 'filter' button I've tried applying a FilterExpression to my DataTable's DefaultView and then assigning that to the grid's DataSource property, but this is causing me problems in PreRender when I try to inspect each row to determine if the row should be placed into edit mode as although I have filtered my DataTable's DefaultView from 10 rows down to 2 rows, in the grid's PreRender event the grid's Items collection still contains 10 items.

What's the recommended approach to manually filtering the grid's data, bearing in mind that I need to access the data in the grid's PreRender event to determine if a filtered row should be in edit mode?

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 22 Jan 2009, 06:02 AM
Hi Rob,

You can either apply  filter expression for the DataTable's DefaultView and bind the Grid with DefaultView or go for the built-in filtering feature for RadGrid. I tried setting the filter expression for the DefaultView and Grid is showing only filtered rows in the Grid's PreRender event.

Here is the code I tried to set filter expression for the defaultview:
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) 
    { 
        string connectionstring = "Data Source=inc140.com; Initial Catalog=NorthWind;User ID=sa; Password=Pass"
        SqlConnection conn = new SqlConnection(connectionstring); 
        SqlDataAdapter adp = new SqlDataAdapter(); 
            conn.Open(); 
            string selectQuery = "SELECT ProductID,ProductName,SupplierID FROM Products"
            adp.SelectCommand = new SqlCommand(selectQuery, conn); 
            DataTable dtTable = new DataTable(); 
            adp.Fill(dtTable); 
            DataView dv = dtTable.DefaultView; 
            dv.RowFilter = "ProductName='Chai'"
            RadGrid1.DataSource = dv; 
            conn.Close(); 
        
    } 

Suppose if you want use the built-in filtering feature with Custom filter options you may consider replacing the items in the filtering menu with your own custom options, and add an event handler to filter the records based on your custom filter functions.
You can refer the following help article to get more details on this.
Custom filter options with handling


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