Grid Lookup for ASP.NET Web Forms - How to filter an editor's data based on selected values of another editor
This example demonstrates how to create a filter expression based on an editor's selected values and use this expression to filter another editor's data.
Create two grid lookup editors and populate them with records. Handle the secondary editor's server-side Init event and add a CustomCallback event handler.
protected void glProducts_Init(object sender, EventArgs e) {
ASPxGridLookup gridLookup = sender as ASPxGridLookup;
gridLookup.GridView.CustomCallback += new ASPxGridViewCustomCallbackEventHandler(gridView_CustomCallback);
if (Session["FilterExpression"] != null) {
gridLookup.GridView.DataSource = adsProducts;
adsProducts.FilterExpression = Session["FilterExpression"].ToString();
}
}To send a callback to the server when the primary editor's selection changes, handle the editor's client-side ValueChanged event.
<dx:ASPxGridLookup ID="glCategories" runat="server" ClientInstanceName="glCategories" SelectionMode="Multiple" ...>
<!-- ... -->
<ClientSideEvents ValueChanged="function(s, e) {
var grid = glProducts.GetGridView();
grid.PerformCallback('ValueChanged');
}" />
</dx:ASPxGridLookup>Handle the secondary editor's CustomCallback event. In the handler, define a filter expression to bind the secondary editor to a data source based on the selected values in the primary editor.
public void gridView_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e) {
if (e.Parameters != "ValueChanged") return;
ASPxGridView grid = sender as ASPxGridView;
var selectedValues = glCategories.GridView.GetSelectedFieldValues(glCategories.KeyFieldName);
if (selectedValues.Count == 0)
selectedValues.Add(-1);
CriteriaOperator selectionCriteria = new InOperator(glCategories.KeyFieldName, selectedValues);
adsProducts.FilterExpression = (GroupOperator.Combine(GroupOperatorType.And, selectionCriteria)).ToString();
Session["FilterExpression"] = adsProducts.FilterExpression;
grid.DataSource = adsProducts;
grid.DataBind();
}- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)
(you will be redirected to DevExpress.com to submit your response)
