Runtime Data Operations in Wijmo GridView
C1GridView has in-built features such as sorting, filtering, paging and grouping. These are extremely helpful for developers as it saves a lot time for them without having to implement it through code. However, it is not always necessary that programmers would bind the grid to a datasource at design time itself. In fact, in most cases data is bound dynamically. This article discusses implementation of sorting, filtering, paging and grouping when C1GridView is bound dynamically.
Binding C1GridView
C1GridView can be bound to an ADO.NET datasource such as DataSet, DataTable, etc. For this sample, we are binding the grid to the ‘Customers’ table in C1NWind.mdb database file.
public DataSet BindGrid()
{
OleDbConnection con = new OleDbConnection("provider=Microsoft.Jet.Oledb.4.0; Data Source=" + Server.MapPath("~/App_Data/C1NWind.mdb"));
OleDbDataAdapter da;
DataSet ds = new DataSet();
da = new OleDbDataAdapter("Select * from Customers", con);
da.Fill(ds);
return ds;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
C1GridView1.DataSource = BindGrid();
C1GridView1.DataBind();
}
}
Handling C1GridView Events
Sorting To implement sorting, we need to handle the Sorting and Sorted events. The grid is rebound in the Sorted event.
protected void C1GridView1_Sorting(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewSortEventArgs e)
{
}
//Handles Sorting
protected void C1GridView1_Sorted(object sender, EventArgs e)
{
C1GridView1.DataSource = BindGrid();
C1GridView1.DataBind();
}
Filtering The code for the filtering the grid is exactly the same as sorting. We need to handle the Filtering and Filtered events.
protected void C1GridView1_Filtering(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewFilterEventArgs e)
{
}
//Handles Filtering
protected void C1GridView1_Filtered(object sender, EventArgs e)
{
C1GridView1.DataSource = BindGrid();
C1GridView1.DataBind();
}
Paging The code for Paging is a little different than sorting and filtering. We only need to handle the Paging event. First of all, set the NewPageIndex as the PageIndex of C1GridView and then rebind the grid as we did earlier.
protected void C1GridView1_PageIndexChanging(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewPageEventArgs e)
{
C1GridView1.PageIndex = e.NewPageIndex;
C1GridView1.DataSource = BindGrid();
C1GridView1.DataBind();
}
Grouping For grouping C1GridView, set AllowColMoving and ShowGroupArea properties to true. We need to handle ColumnGrouped and ColumnUngrouped events where ColumnUngrouped event is left blank. However, in the ColumnGrouped event we must rebind the grid, this time with a parameter. The parameter is the HeaderText of the Column which is being dragged for Grouping. This parameter is used for Sorting the grid first by this column and then applying grouping so that duplicate groups are not created.
//Handles Column Grouping
protected void C1GridView1_ColumnGrouped(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewColumnGroupedEventArgs e)
{
C1GridView1.DataSource = BindGrid(e.Drag.HeaderText);
C1GridView1.DataBind();
}
//Handles Column UnGrouping
protected void C1GridView1_ColumnUngrouped(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewColumnUngroupedEventArgs e)
{
}