Articles in this section
Category / Section

How to return unique values from server end for excel filter checkbox in ASP.NET WebForms Grid?

1 min read

This knowledge base explains how to return unique values from the server end for excel filter checkbox values.

Solution:

By default, if we bind more than 1000 records in Grid then the excel filter will show only distinct values from the first 1000 records of Grid dataSource. If we have one distinct value from the first 1000 records, then it will return only one distinct value from server end.

To overcome this, we need to return the 1000 (default value of MaximunFilterChoice) distinct values of entire Grid dataSource from the server, by passing the filtering column name to server end through the actionBegin event of ejGrid and based on that we can get the distinct values from server end.

  1. The Grid initialization as follows.

HTML

<div id="FlatGrid"></div>

 

JS

<script type="text/javascript">
    $(function () {
        $("#FlatGrid").ejGrid({
            dataSource: ej.DataManager({
                url: "/Grid/UrlDataSource",
                adaptor: "UrlAdaptor"
            }),
            allowPaging: true,
            allowFiltering: true,
            filterSettings: {filterType:'excel'},
            columns: [
                  { field: "OrderID", headerText: "Order ID", width: 100 },
                  { field: "CustomerID", headerText: "Customer ID", width: 100 },
                  { field: "EmployeeID", headerText: "Employee ID", width: 100 }
            ],
            actionBegin: "onActionBegin"
        });
    });
</script>

 

MVC

@(Html.EJ().Grid<OrdersView>("FlatGrid")
     .Datasource(e => e.URL("/Grid/UrlDataSource").Adaptor(AdaptorType.UrlAdaptor))
    .AllowFiltering()
    .AllowPaging()
    .FilterSettings(e => e.FilterType(FilterType.Excel))
    .ClientSideEvents(cs => cs.ActionBegin("onActionBegin"))
    .Columns(col =>
    {
        col.Field("OrderID").HeaderText("Order ID").Width(100).Add();
        col.Field("CustomerID").HeaderText("Customer ID").Width(100).Add();
        col.Field("EmployeeID").HeaderText("Employee ID").Width(100).Add();
    })
    )

 

 

[CS]
 
namespace SyncfusionMvcApplication16.Controllers
{
    public class GridController : Controller
    {
        //
        // GET: /Grid/
        public List<Orders> order = new List<Orders>();
        public ActionResult GridFeatures()
        { 
            return View();
        }
        public ActionResult UrlDataSource(DataManager dm)
        {
            BindDataSource();
            IEnumerable data = order;
            DataOperations operation = new DataOperations();
            if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
            {
                data = operation.PerformSorting(data, dm.Sorted);
            }
            if (dm.Where != null && dm.Where.Count > 0) //Filtering
            {
                data = operation.PerformWhereFilter(data, dm.Where, dm.Where[0].Operator);
            }
            if (dm.Search != null && dm.Search.Count > 0) //Searching
            {
                data = operation.PerformSearching(data, dm.Search);
 
            }
            int count = data.Cast<Orders>().Count();
 
            if (dm.Select != null)
            {
                data = operation.PerformSelect(data, dm.Select);
                data = data.Cast<dynamic>().Distinct().AsEnumerable();
            }
            if (dm.Skip != 0)
            {
                data = operation.PerformSkip(data, dm.Skip);
            }
            if (dm.Take != 0)
            {
                data = operation.PerformTake(data, dm.Take);
            }
            if (dm.RequiresCounts)
                return Json(new { result = data, count = count });
 
            else
                return Json(data, JsonRequestBehavior.AllowGet);
        }
        private void BindDataSource()
        {
            int code = 0;
            if (order.Count() == 0)
            {
                for (int i = 1; i < 17000; i++)
                {
                    order.Add(new Orders(code + 1, "Budget", i + 0));
                }
                for (int j = 17000; j < 29000; j++)
                {
                    order.Add(new Orders(code + 1, "Forecast", j + 0));
                }
                for (int k = 29000; k < 38000; k++)
                {
                    order.Add(new Orders(code + 1, "S&OP", k + 0));
                }
            }
 
        }
 
        [Serializable]
        public class Orders
        {
            public Orders()
            {
 
            }
            public Orders(int orderId, string customerId, int empId)
            {
                this.OrderID = orderId;
                this.CustomerID = customerId;
                this.EmployeeID = empId;
            }
            public int OrderID { get; set; }
            public string CustomerID { get; set; }
            public int EmployeeID { get; set; }
        }
    }
}

 

ASP

<ej:Grid ID="FlatGrid" runat="server" AllowPaging="true" AllowFiltering="true">
        <FilterSettings FilterType="Excel"></FilterSettings>
        <ClientSideEvents ActionBegin="onActionBegin" />
          <DataManager URL="GridFeatures.aspx/UrlDataSource" Adaptor="WebMethodAdaptor" />
            <Columns>
                <ej:Column Field="OrderID" HeaderText="Order ID" Width="100" />
                <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="100" />
                <ej:Column Field="EmployeeID" HeaderText="Employee ID" Width="100" />
           </Columns>
      </ej:Grid>

 

 

[CS]
 
namespace SyncfusionMvcApplication16.Controllers
{
    public class GridController : Controller
    {
        public static List<Orders> order = new List<Orders>();
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static object UrlDataSource(DataManager value)
        {
            BindDataSource();
            IEnumerable data = order;
            DataOperations operation = new DataOperations();
            if (value.Sorted != null && value.Sorted.Count > 0) //Sorting
            {
                data = operation.PerformSorting(data, value.Sorted);
            }
            if (value.Where != null && value.Where.Count > 0) //Filtering
            {
                data = operation.PerformWhereFilter(data, value.Where, value.Where[0].Operator);
            }
            if (value.Search != null && value.Search.Count > 0) //Searching
            {
                data = operation.PerformSearching(data, value.Search);
 
            }
            int count = data.Cast<Orders>().Count();
 
            if (value.Select != null)
            {
                data = operation.PerformSelect(data, value.Select);
                data = data.Cast<dynamic>().Distinct().AsEnumerable();
            }
            if (value.Skip != 0)
            {
                data = operation.PerformSkip(data, value.Skip);
            }
            if (value.Take != 0)
            {
                data = operation.PerformTake(data, value.Take);
            }
            if (value.RequiresCounts)
                return new { result = data, count = count };
            else
                return data;
        }
        private static void BindDataSource()
        {
            int code = 0;
            if (order.Count() == 0)
            {
                for (int i = 1; i < 17000; i++)
                {
                    order.Add(new Orders(code + 1, "Budget", i + 0));
                }
                for (int j = 17000; j < 29000; j++)
                {
                    order.Add(new Orders(code + 1, "Forecast", j + 0));
                }
                for (int k = 29000; k < 38000; k++)
                {
                    order.Add(new Orders(code + 1, "S&OP", k + 0));
                }
            }
        }
 
        [Serializable]
        public class Orders
        {
            public Orders()
            {
 
            }
            public Orders(int orderId, string customerId, int empId)
            {
                this.OrderID = orderId;
                this.CustomerID = customerId;
                this.EmployeeID = empId;
            }
            public int OrderID { get; set; }
            public string CustomerID { get; set; }
            public int EmployeeID { get; set; }
        }    
    }
}

 

CORE

<ej-grid id="FlatGrid" allow-paging="true" action-begin="onActionBegin" allow-filtering="true">
    <e-filter-settings filter-type="Excel"></e-filter-settings>
    <e-datamanager url="UrlDataSource" adaptor="UrlAdaptor"></e-datamanager>
    <e-columns>
        <e-column field="OrderID" header-text="Order ID" width="100"></e-column>
        <e-column field="CustomerID" header-text="Customer ID" width="100"></e-column>
        <e-column field="EmployeeID" header-text="Employee ID" width="100"></e-column>
    </e-columns>
</ej-grid>

 

 

[CS]
 
namespace SyncfusionMvcApplication16.Controllers
{
    public class GridController : Controller
    {
        public static List<Orders> order = new List<Orders>();
        public IActionResult Index()
        {
            return View();
        }
        public IActionResult UrlDataSource([FromBody] DataManager dm)
        {
            BindDataSource();
            IEnumerable data = order;
            DataOperations operation = new DataOperations();
            if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
            {
                data = operation.PerformSorting(data, dm.Sorted);
            }
            if (dm.Where != null && dm.Where.Count > 0) //Filtering
            {
                data = operation.PerformWhereFilter(data, dm.Where, dm.Where[0].Operator);
            }
            if (dm.Search != null && dm.Search.Count > 0) //Searching
            {
                data = operation.PerformSearching(data, dm.Search);
            }
            int count = data.Cast<Orders>().Count();
 
            if (dm.Select != null)
            {
                data = operation.PerformSelect(data, dm.Select);
                data = data.Cast<dynamic>().Distinct().AsEnumerable();
            }
            if (dm.Skip != 0)
            {
                data = operation.PerformSkip(data, dm.Skip);
            }
            if (dm.Take != 0)
            {
                data = operation.PerformTake(data, dm.Take);
            }
            if (dm.RequiresCounts)
                return Json(new { result = data, count = count });
            else
                return Json(data);
        }
 
        private void BindDataSource()
        {
            int code = 0;
            if (order.Count() == 0)
            {
                for (int i = 1; i < 17000; i++)
                {
                    order.Add(new Orders(code + 1, "Budget", i + 0));
                }
                for (int j = 17000; j < 29000; j++)
                {
                    order.Add(new Orders(code + 1, "Forecast", j + 0));
                }
                for (int k = 29000; k < 38000; k++)
                {
                    order.Add(new Orders(code + 1, "S&OP", k + 0));
                }
            }
        }
 
        [Serializable]
        public class Orders
        {
            public Orders()
            {
            }
            public Orders(int orderId, string customerId, int empId)
            {
                this.OrderID = orderId;
                this.CustomerID = customerId;
                this.EmployeeID = empId;
            }
            public int OrderID { get; set; }
            public string CustomerID { get; set; }
            public int EmployeeID { get; set; }
        }   
    }
}

 

Angular

<ej-grid id="FlatGrid" [dataSource]="DataManager" [allowPaging]="true" 
[allowFiltering]="true" [filterSettings]="filterType" > 
    <e-columns>
        <e-column field="OrderID" headerText="Order ID" width="100"></e-column>
        <e-column field="CustomerID" headerText="Customer ID" width="100"></e-column>
        <e-column field="EmployeeID" headertext="Employee ID" width="100" ></e-column>
    </e-columns>
 </ej-grid>

 

TS

export class AppComponent {   
     public filterType;
     public DataManager : any;
constructor() {     
     this.DataManager = new ej.DataManager({ url: "/../Grid/UrlDataSource" , adaptor: new ej.UrlAdaptor() });  
 
     this.filterType = { filterType: "excel" };           
     }
}

 

  1. In the Client side actionBegin event we can get the filtering column name and we set it to the select query of Grid.

 

<script type="text/javascript">
    function onActionBegin(args) {
        if (args.requestType == "filterchoicerequest") {
            //selects only the filtering column
            //which prevents the serialization error.
            args.query.select(args.filterModel.fName);
        }
    }
</script>

 

Result:

Figure 1: Shown some unique values in excel filter dialog for CustomerID column in Grid.


Note:

A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.

The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.

You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied