Suppose there are 1000s of rows are available in webgrid and you want to find the some particular set of results. For an example employee name starts with "par" then given this string in TextBox. On button click, this code will apply a filter to the DataView by clearing the Grid rows and then rebind it explicitly to get the results.
Complete Code:
----------------
publicWebGrid_AJAX_Filtering_DataView_2005V2_CS.DataSetNorthWind.MyDataTable
{
get
{
if (Session["data"] == null)
{
this.sqlDataAdapterOrders.Fill(this.dataSetNorthWind1.Orders);
this.dataSetNorthWind1.Orders.DefaultView.Sort = "ID ASC";
Session["data"] = this.dataSetNorthWind1.Orders;
}
return (WebGrid_AJAX_Filtering_DataView_2005V2_CS.DataSetNorthWind.MyDataTable)Session["data"];
}
}
private void UltraWebGrid1_InitializeDataSource(object sender, Infragistics.WebUI.UltraWebGrid.UltraGridEventArgs e)
{
this.UltraWebGrid1.DataSource = this.DefaultView;
}
private void Button1_Click(object sender, System.EventArgs e)
{
this.UltraWebGrid1.Rows.Clear();
if (this.TextBox1.Text == "")
{
this.Orders.DefaultView.RowFilter = "";
}
else
{
//This is the main line which filters data based on specified string in textbox
this.Orders.DefaultView.RowFilter = "ID LIKE '%" + this.TextBox1.Text+ "%'" ;
}
this.Label2.Text = this.Orders.DefaultView.Count + " Records Found";
this.UltraWebGrid1.DataSource = this.DefaultView;
this.UltraWebGrid1.DataBind();
}
----------------
publicWebGrid_AJAX_Filtering_DataView_2005V2_CS.DataSetNorthWind.MyDataTable
{
get
{
if (Session["data"] == null)
{
this.sqlDataAdapterOrders.Fill(this.dataSetNorthWind1.Orders);
this.dataSetNorthWind1.Orders.DefaultView.Sort = "ID ASC";
Session["data"] = this.dataSetNorthWind1.Orders;
}
return (WebGrid_AJAX_Filtering_DataView_2005V2_CS.DataSetNorthWind.MyDataTable)Session["data"];
}
}
private void UltraWebGrid1_InitializeDataSource(object sender, Infragistics.WebUI.UltraWebGrid.UltraGridEventArgs e)
{
this.UltraWebGrid1.DataSource = this.DefaultView;
}
private void Button1_Click(object sender, System.EventArgs e)
{
this.UltraWebGrid1.Rows.Clear();
if (this.TextBox1.Text == "")
{
this.Orders.DefaultView.RowFilter = "";
}
else
{
//This is the main line which filters data based on specified string in textbox
this.Orders.DefaultView.RowFilter = "ID LIKE '%" + this.TextBox1.Text+ "%'" ;
}
this.Label2.Text = this.Orders.DefaultView.Count + " Records Found";
this.UltraWebGrid1.DataSource = this.DefaultView;
this.UltraWebGrid1.DataBind();
}
No comments:
Post a Comment