I am trying Sorting functionality in Grid view but its not working.Can some body help?
Code:
private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
protected DataSet FillDataSet()
{
string source = "Database=GridTest;Server=Localhost;Trusted_Connection=yes";
con = new SqlConnection(source);
cmd = new SqlCommand("proc_mygrid", con);
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
return ds;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = GridView1.DataSource as DataTable;
if (dt != null)
{
DataView dv = new DataView(dt);
dv.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
GridView1.DataSource = dv;
GridView1.DataBind();
}
Here dt is coming null.why? pls help thanks.
EDIT:
enter code here <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AllowPaging="true" AllowSorting="true" PageSize="12"
onpageindexchanging="GridView1_PageIndexChanging"
onsorting="GridView1_Sorting">
EDIT(Total code)
public partial class _Default : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
DataSet ds;
SqlDataAdapter da;
protected void Page_Load(object sender, EventArgs e)
{
}
private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
protected DataSet FillDataSet()
{
string source = "Database=GridTest;Server=Localhost;Trusted_Connection=yes";
con = new SqlConnection(source);
cmd = new SqlCommand("proc_mygrid", con);
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
return ds;
}
protected void GetValues(object sender, EventArgs e)
{
FillDataSet();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int newPagenumber = e.NewPageIndex;
GridView1.PageIndex = newPagenumber;
GridView1.DataSource = FillDataSet();
GridView1.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataSet ds = FillDataSet();
DataTable dt = ds.Tables[0];
if (dt != null)
{
dt.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
GridView1.DataSource = dt;
GridView1.DataBind();
}
CODE:
private string GetSortDirection(string column)
{
string sortDirection = "DESC";
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "DESC"))
{
sortDirection = "ASC";
}
}
}
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = ((DataSet)Session["myDataSet"]).Tables[0];
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
GridView1.DataSource = dt;
GridView1.DataBind();
}
Because you're setting a DataSet as DataSource and then casting it to DataTable with the operator as.
The 'as' operator in C# is a tentative cast - if it's impossible to cast (types are not compatible) instead of throwing an exception as the direct cast the 'as' operator sets the reference to null.
If you only have one datatable in your dataset then you can get the first element like this:
ds.Tables[0];
... or use the name of the table:
ds.Tables["myTable"];
in your case you can try...
DataTable dt = GridView1.DataSource.Tables[0] as DataTable;
Hope it helps!
EDIT:
with regards to your sorting problem (once you get the datatable):
if (dt != null)
{
dt.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
GridView1.DataBind();
}
You can do this because dt is a reference to the exact same object that's already set as DataSource for your grid. It should do the trick - if not there's smt else I am missing (such as we're sorting the wrong table meaning you have more than one table in the DataSet).
EDIT:
had a look at your code. I don't know exactly when GetValues gets fired but I suspect it's causing your problem (I think it might be overriding your sorting or smt along those lines).
If you comment out FillDataSource from getValues and modify your PageLoad to do this:
void Page_Load(Object sender, EventArgs e)
{
// Load data only once, when the page is first loaded.
if (!IsPostBack)
{
Session["myDataSet"] = FillDataSet();
}
}
then in your sort method you retrieve the DataSource like this:
DataTable dt = ((DataSet)Session["myDataSet"]).Tables[0];
Also you can retrieve the DataSet from session in your pagination handler method.
You should also notice an improvement in performance since you're retrieving the stuff form the db just once.
Give it a shot!
In Testing.aspx.cs
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (dataset != null)
{
datatable = dataset.Tables[0];
DataView dataView = new DataView(datatable);
dataView.Sort = e.SortExpression;
GridView1.DataSource = dataView;
GridView1.DataBind();
}
}
In Testing.aspx
<asp:GridView ID="GridView1" runat="server"
AllowSorting="True" OnSorting="GridView1_Sorting">
Related
I work in C-sharp on ASP NET 4.
I need to add a sorting function to a column in a GridView. I've set the AllowSorting-property on the GridView to true and added sort expressions to the column.
Unfortunately, the sorting isn't working in the GridView.
Below is my code-behind-file for the column that should be able to sort, but I get the error
CS1502: The best overloaded method match for 'System.Data.DataView.DataView(System.Data.DataTable)' has some invalid arguments
on this line:
DataView sortedView = new DataView(BindData());
Code behind:
string sortingDirection;
public SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
public string SortField
{
get
{
return (string)ViewState["SortField"] ?? "Name";
}
set
{
ViewState["SortField"] = value;
}
}
protected void gvProducts_Sorting(object sender, GridViewSortEventArgs e)
{
sortingDirection = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
sortingDirection = "Desc";
}
else
{
dir = SortDirection.Ascending;
sortingDirection = "Asc";
}
DataView sortedView = new DataView(RetrieveProducts());
sortedView.Sort = e.SortExpression + " " + sortingDirection;
SortField = e.SortExpression;
gvProducts.DataSource = sortedView;
gvProducts.DataBind();
}
protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (dir == SortDirection.Ascending)
{
sortingDirection = "Asc";
}
else
{
sortingDirection = "Desc";
}
DataView sortedView = new DataView(RetrieveProducts());
sortedView.Sort = SortField + " " + sortingDirection;
gvProducts.DataSource = sortedView;
gvProducts.PageIndex = e.NewPageIndex;
gvProducts.DataBind();
}
private void BindData()
{
gvProducts.DataSource = RetrieveProducts();
gvProducts.DataBind();
}
private DataSet RetrieveProducts()
{
DataSet dsProducts = new DataSet();
string sql = " ... ";
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand(sql, cn))
{
........
}
}
return dsProducts;
}
Edit #1
DataView sortedView = new DataView(dsProducts.Tables[0]);
Edit # 2
I have added in aspx page:
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
But If clicked in Column Name I have this new error:
System.IndexOutOfRangeException: Cannot find table 0.
on this line:
Line 100: DataView sortedView = new DataView(dsProducts.Tables[0]);
The DataView class has only three constructors, in which one is default constructor DataView(), second one takes a DataTable as an argument DataView(DataTable), the other one takes four arguments DataView(DataTable, String, String, DataViewRowState).
The DataView constructor expects arguments of any one of these types, but your code has argument of some other type. That's the error.
Your BindData method should return a DataTable object,
//This function should return a Datatable
private void BindData()
{
gvProducts.DataSource = RetrieveProducts();
gvProducts.DataBind();
}
which you can pass into your DataView here.
DataView sortedView = new DataView(BindData());
For your second edit,
System.IndexOutOfRangeException: Cannot find table 0.
on this line:
Line 100: DataView sortedView = new DataView(dsProducts.Tables[0]);
I guess the dataset is empty, the error clearly states that there isn't any table in the dataset at position 0. So check whether your dataset has tables or not. Might be your sql request didn't get any table to fill in the dataset.
Else you might have created a new instance of the dataset.
I need to filter a gridview that retreives filtered data from a table. Therefore I bound the gridview to a dataset. Now i can't seem to find a solution to filter it further.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
SqlConnection myCon = new SqlConnection(connectionstring);
SqlDataAdapter adapter = new SqlDataAdapter(cmd, myCon);
adapter.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//need to insert code here for filtering GridView1 based on TextBox1.Text
}
Thanks for the help.
Try this:
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
SqlConnection myCon = new SqlConnection(connectionstring);
SqlDataAdapter adapter = new SqlDataAdapter(cmd, myCon);
adapter.Fill(ds);
DataView view = new DataView();
view.Table = ds.Tables[0];
view.RowFilter = "ColumnName = " + TextBox1.Text.Trim();
GridView1.DataSource = view;
GridView1.DataBind();
}
you have to refactor your code.
Here's a complete sample which handles GridView's paging, sorting(both directions) and filtering(two columns).
// store sorting across postbacks in a ViewState variable
public string SortExpression
{
get
{
if (ViewState["GridSort"]== null)
{
ViewState["GridSort"] = "Column1 ASC";
}
return ViewState["GridSort"].ToString();
}
set { ViewState["GridSort"] = value; }
}
protected void Page_load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
Here's the main method that does all (including the filter-function):
private void BindGrid()
{
try
{
var tblData = new DataTable();
var filter1 = TextBox1.Text.Trim();
var filter2 = TextBox2.Text.Trim();
using (var sqlCon = new System.Data.SqlClient.SqlConnection(connectionstring))
{
String sql = String.Empty;
var sqlCmd = new System.Data.SqlClient.SqlCommand();
if (filter1.Length != 0 && filter2.Length != 0)
{
sql = "SELECT Column1,Column2 FROM Table WHERE Column1 LIKE #Column1 AND Column2 LIKE #Column2 ORDER BY {0}";
sqlCmd.Parameters.AddWithValue("#Column1", string.Format("%{0}%", filter1));
sqlCmd.Parameters.AddWithValue("#Column2", string.Format("%{0}%", filter2));
}
else if (filter1.Length != 0)
{
sql = "SELECT Column1,Column2 FROM Table WHERE Column1 LIKE #Column1 ORDER BY {0}";
sqlCmd.Parameters.AddWithValue("#Column1", string.Format("%{0}%", filter1));
}
else if (filter2.Length != 0)
{
sql = "SELECT Column1,Column2 FROM Table WHERE Column2 LIKE #Column2 ORDER BY {0}";
sqlCmd.Parameters.AddWithValue("#Column2", string.Format("%{0}%", filter2));
}
else
{
// no filter, select all
sql = "SELECT Column1,Column2 FROM Table ORDER BY {0}";
}
sqlCmd.CommandText = string.Format(sql, this.SortExpression);
sqlCmd.Connection = sqlCon;
using (System.Data.SqlClient.SqlDataAdapter objAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd))
{
objAdapter.Fill(tblData);
}
}
GridView1.DataSource = tblData;
GridView1.DataBind();
}
catch (Exception)
{
// log
throw;
}
}
Paging:
private void GridView1_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
Filter-Button-Click:
private void BtnFilter_Click(object sender, System.EventArgs e)
{
BindGrid();
}
Sorting:
protected void GridView1_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
{
string currentSortColumn = null;
string currentSortDirection = null;
currentSortColumn = this.SortExpression.Split(' ')[0];
currentSortDirection = this.SortExpression.Split(' ')[1];
if (e.SortExpression.Equals(currentSortColumn))
{
//switch sort direction
switch (currentSortDirection.ToUpper())
{
case "ASC":
this.SortExpression = currentSortColumn + " DESC";
break;
case "DESC":
this.SortExpression = currentSortColumn + " ASC";
break;
}
}
else
{
this.SortExpression = e.SortExpression + " ASC";
}
BindGrid();
}
Just converted from VB manually, so i hope there are no remaining errors.
sql = new SqlConnection(Connection.con);
adapter = new SqlDataAdapter(#"select EntryID * from Table where Name like #Name ", sql);
adapter.SelectCommand.Parameters.AddWithValue("#Name", string.Format("%{0}%", textBox1.Text));
dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
See the below code, I am try to sorting Grid view data by calling function provided by the Microsoft vs 2008, but the paging is done well but the sorting process is not work, tell me where should i change the following code and yes i put grid view in updated penal,is there problem regarding to update penal?
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
showData();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
SqlCommand cmd = new SqlCommand("showData", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0] != null)
{
ds.Tables[0].DefaultView.Sort = e.SortExpression + " " + sortType(e.SortDirection);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
}
}
private string sortType(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "DESC";
break;
case SortDirection.Descending:
newSortDirection = "ASC";
break;
}
return newSortDirection;
}
Can you ensure AllowSorting="true" and OnSorting="GridView1_Sorting"
Also refer
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx
http://www.codeproject.com/Articles/26882/Gridview-Sorting-with-Up-and-Down-Icons-Paging
sorting and paging with gridview asp.net
http://www.c-sharpcorner.com/uploadfile/afenster/gridview-sorting/
Hope this helps
i think you have to assign dataview to the DataSource property of gridview, not the dataset object. like this
DataTable dt = ds.Tables[0];
DataView dv = new DataView(dt);
dv.Sort = e.SortExpression + " " + sortType(e.SortDirection);
GridView1.DataSource = dv;
GridView1.DataBind();
you are missing a default value for newSortDirection.
private string sortType(SortDirection sortDirection)
{
string newSortDirection = "ASC";
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "DESC";
break;
case SortDirection.Descending:
newSortDirection = "ASC";
break;
}
return newSortDirection;
}
I have asp.net page contain gridview as following
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
OnPageIndexChanging="gridView_PageIndexChanging"
OnSorting="TaskGridView_Sorting"
AllowSorting="True" AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2">
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<Columns>
<asp:boundfield datafield="name_english" convertemptystringtonull="true" headertext="Name"/>
<asp:BoundField DataField="Inc_ID" convertemptystringtonull="true" HeaderText="Inc_ID" SortExpression="Inc_ID"/>
<asp:BoundField DataField="UID" HeaderText="Study_UID" SortExpression= "UID"/>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
I fill and sort it using the following code
protected void Button1_Click(object sender, EventArgs e)
{
//connection to database
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["NorthindConnectionString"].ConnectionString;
SqlConnection myConn = new SqlConnection(connection);
myConn.Open();
SqlCommand cmd = new SqlCommand(" WorkList", myConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("#Name", TextBox1.Text));
cmd.Parameters.Add(new SqlParameter("#ID", TextBox2.Text));
cmd.Parameters.Add(new SqlParameter("#AccNo", TextBox4.Text));
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
Session["TaskTable"] = ds.Tables[0];
ds.Dispose();
da.Dispose();
GridView1.Visible = true;
myConn.Close();
}
protected void TaskGridView_Sorting(object sender, GridViewSortEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = Session["TaskTable"] as DataTable;
if (dt != null)
{
//Sort the data.
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
GridView1.DataSource = Session["TaskTable"];
GridView1.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;
}
}
the problem when click on any header for sorting raise error System.IndexOutOfRangeException: Cannot find column name .. , any idea to solve that , I am sure from the columns name in database ,
private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
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();
}
}
try this code..
You need to bind your grid to the sorted view (and not the original table) for sorting to work.
protected void TaskGridView_Sorting(object sender, GridViewSortEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = Session["TaskTable"] as DataTable;
if (dt != null)
{
//Sort the data.
dt.DefaultView.Sort = e.SortExpression;
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
}
}
I am not sure if you need GetSortDirection method.
Also note that SortExpression property consists of sort direction (e.g. "UID DESC") so base your logic on that. Your code could have set sort expression such as "UID DESC ASC" which is obviously a wrong expression.
In order to have the code work in asp.net or a web environment, you need to put your gridview in Session a session object, and your sorting in a view state. In order to get the sorting to work with paging, do the following.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["SearchTable"] = gv_GridView.DataSource;
LoadSearchGrid("Select * from WF_Search);
}
private void LoadSearchGrid(string query)
{
DataTable dsp = new DataTable();
conn = new SqlConnection(ConnectionString);
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
conn.Open();
sda.Fill(dsp);
Session["SearchTable"] = dsp;
gv_GridView.DataSource = Session["SearchTable"];
gv_GridView.DataBind();
conn.Close();
sda.Dispose();
}
protected void gv_GridView_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["SortDirection"] = e.SortDirection;
DataTable dtr = Session["SearchTable"] as DataTable;
if (dtr != null)
{
dtr.DefaultView.Sort = e.SortExpression + " " + getSortDirection(e.SortExpression);
gv_GridView.DataSource = Session["SearchTable"];
gv_GridView.DataBind();
Session["SearchTable"] = gv_GridView.DataSource;
}
}
private string getSortDirection(string column)
{
string sortDirection = "ASC";
string sortExpression = ViewState["SortDirection"] as string;
if (sortExpression != null)
{
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if (lastDirection != null && lastDirection == "ASC")
{
sortDirection = "DESC";
}
}
}
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
protected void gv_GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv_GridView.DataSource = Session["SearchTable"];
gv_GridView.DataBind();
gv_GridView.PageIndex = e.NewPageIndex;
}
This is my code code for the Page_Load Event
OdbcConnection myConnection;
DataSet dataSet = new DataSet();
OdbcDataAdapter adapter;
//making my connection
myConnection = new OdbcConnection(ConfigurationManager.ConnectionStrings ["ODBC_ConnectionString"].ConnectionString);
adapter = new OdbcDataAdapter("SELECT * from Company", myConnection);
adapter.Fill(dataSet, "MyData");
GridView1.DataSource = dataSet;
Session["DataSource"] = dataSet;
GridView1.DataBind();
This is my code for the PageIndexChanging event and it all works fine.
DataSet ds = new DataSet();
if (Session["DataSource"] != null)
ds = ((DataSet)Session["DataSource"]);
GridView1.DataSource = ds;
GridView1.PageIndex = e.NewPageIndex;
this.GridView1.DataBind();
Now what code do i need to create the Sorting event?
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
?????????????????????????
}
Etienne
I usually do this:
public string SortField {
get {
return (string) ViewState["_sortField"];
}
set {
ViewState["_sortField"] = value;
}
}
public string SortDir {
get {
return (string) ViewState["_sortDir"];
}
set {
ViewState["_sortDir"] = value;
}
}
Put your code to do databinding into another Method because you have to call it during Sort, Paging, and when your page first loads. Call it DoDataBind() for example. Then you have in DoDataBind()
DataTable dt = yourDataSet.Tables[0];
dt.DefaultView.Sort = SortField + " " + SortDir;
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
Then your event looks like this:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) {
if (e.SortExpression == SortField && SortDir != "desc") {
SortDir = "desc";
}
else {
SortDir = "asc";
}
SortField = e.SortExpression;
DoDataBind();
}
Then in your aspx page you'll need to specify what the SortExpression is. For example something like this:
<asp:BoundField DataField="FIRSTNAME"
HeaderText="First Name" SortExpression="FIRSTNAME" />
You can filter and sort your dataset using:
ds.Tables[0].Select(filterExp, sortExp, etc...);