Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0

friends I want to sort or filter the gridview on textbox text changing value using Ajax in Asp.net
Posted 21-Mar-12 23:55pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

for sorting, sort source data table. which is more helpful
 
Eg..
 
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = gridView.DataSource as DataTable;

if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

gridView.DataSource = dataView;
gridView.DataBind();
}
}
  Permalink  
Comments
sameer hilal at 22-Mar-12 6:08am
   
===============
The code below works when columns are created explicitly as declerative >><asp:BoundField DataField="ID" HeaderText="ID" SortExpression="" />
===============
 
AllowSorting
 
="True"
OnSorting
 
="GvEvent_Sorting"
 
<
 
asp:TemplateField ItemStyle-HorizontalAlign="Right" HeaderText="MyDate" SortExpression
 
="MyDate">
 
#region GV_sort event and Sort Direction
 
protected void GvEvent_Sorting(object sender, GridViewSortEventArgs e)
{
 
//Retrieve the table from the session object.
DataTable dt = Session["MyDS"] as DataTable;
if (dt != null)
{
//Sort the data.
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
GridView Gv = (GridView)sender;
Gv.DataSource = Session["MyDS"];
Gv.DataBind();
}
}
 
private string GetSortDirection(string column)
{
// By default, set the sort direction to ascending.
string sortDirection = "ASC";
// Retrieve the last column that was sorted.
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
// Check if the same column is being sorted.
// Otherwise, the default value can be returned.
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection = "DESC";
}
}
}
// Save new values in ViewState.
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
#endregion
sameer hilal at 22-Mar-12 6:15am
   
For filtering filter using session which will hold value after postback
 
You have to write textbox value in session
 
write code where you using sql expression..
 
Eg..
 
String sql="Select * From Table_Name ";
 
If(!String.IsNullOrEmpty(txtFirstname.text))
{
sql+= txtFirstname.text;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 449
1 Richard MacCutchan 240
2 OriginalGriff 230
3 Jared Sanow 145
4 Vinay Mistry 143
0 Sergey Alexandrovich Kryukov 5,955
1 OriginalGriff 4,805
2 CPallini 2,473
3 Abhinav S 1,507
4 Richard MacCutchan 1,437


Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 22 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100