ASP.net Gridview not sorting - c#

I have a gridview that I'd like to sort. I've read a few tutorials and copied most of my code directly from the MSDN page, but I can't get it to work. It compiles, but nothing happens when I click the grid column headers.
My HTML:
<asp:DataGrid runat="server" id="dgrMainGrid" CssClass="c_mainGrid"
AutoGenerateColumns="true" AllowSorting="true"
OnSorting="TaskGridView_Sorting" />
My Codebehind:
protected void TaskGridView_Sorting(object sender, GridViewSortEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = Session["Grid"] as DataTable;
if (dt != null)
{
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
dgrMainGrid.DataSource = dt;
dgrMainGrid.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";
}
}
}
return sortDirection;
}
I know the data table in my session variable works, because it's the source for my grid on page load, which works fine. One other thing, if it's important, this gridview resides in an update panel.
As I said, most of this is copied from the MSDN page, and I've been through it until I'm going code-blind. Can someone see my mistake? Thanks.

You are using a DataGrid, not a GridView. According to Microsoft, DataGrid does not have a OnSorting event, but an OnSortCommand. Either use that or switch to GridView (recommended)
<asp:DataGrid runat="server" OnSortCommand="dgrMainGrid_SortCommand" id="dgrMainGrid" CssClass="c_mainGrid" AutoGenerateColumns="true" AllowSorting="true" />
And in code behind
protected void dgrMainGrid_SortCommand(object source, DataGridSortCommandEventArgs e)
{
DataTable dt = Session["Grid"] as DataTable;
if (dt != null)
{
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
dgrMainGrid.DataSource = dt;
dgrMainGrid.DataBind();
}
}
And your GetSortDirection does not work as it should. See this example.

Related

GridView loses ViewState "SortExpression" after DataBind

I have a page that allows both sorting columns as well as a search option that filters the GridView data. The problem I am running into is the GridView's ViewState SortExpression gets lost when filtering the datatable through the Search option.
My question is how do I retain the ViewState after a DataBind to ensure the SortExpression is kept on the newly binded dataTable?
protected void Page_Load(object sender, EventArgs e)
{
try{
if (IsPostBack)
{
Control control = null;
string controlName = Request.Params["__EVENTTARGET"];
if (!String.IsNullOrEmpty(controlName))
{
control = FindControl(controlName);
GridViewRow gvRow1 = (GridViewRow)control.Parent.Parent;
string controlID = control.ID.ToString();
}
}
if(!IsPostBack)
{
DataGrid_Load(DAL.reg(HeadText.Text, OrgText.Text), "reg");
}
}
catch{}
}
private void DataGrid_Load(DataTable command, string type)
{
DataTable dataTable = new DataTable();
dataTable = command;
string sortDir = ViewState["SortDirection"] as string;
string sortExp = ViewState["SortExpression"] as string;
if(ViewState["SortExpression"] != null)
{
dataTable = resort(dataTable, sortExp, sortDir);
}
string myStatus = HeadText.Text;
DataRow[] dr = dataTable.Select("status = '" + myStatus + "'");
DataTable filteredDataTable = dataTable.Clone();
foreach (DataRow sourceRow in dr)
{
filteredDataTable.ImportRow(sourceRow);
}
GridView1.DataSource = filteredDataTable;
GridView1.DataBind();
}
public class dal
{
public DataTable reg(string head, string org = null)
{
if (head == "all")
return Data_Load(String.Format("SELECT * from reg"), "reg");
}
}
I resolved this and it was simply a problem with my Load Data procedure which did not take into account if there was something already filtered in the GridView and the filter was being lost on the new Load.
To fix this I made a static searchFilter variable and added a check for this in each of the methods that called for loading data into the GridView:
after column sorting
after the "Rows Per Page" is changed
Any other functions within that page that require Loading Data on that page.
The code looked this this in each of the functions that load data:
if(searchFilter != "")
loadDataWithFilter();
else
loadDataWithoutFilter();
I did not have to do anything with the Page_Load PostBack.

Sorting a gridview control - The direction never changes

I can't seem to get this right, even after looking at a few examples.
I've got this code, which happily re-sorts my gridview in an Ascending order:
// gridViewSorting and ConvertSortDirectionToSql are both necessary to ensure the gridview can sort when their column headers are
// clicked. You must remember to add (AllowSorting="True" OnSorting="gridViewSorting") to the gridview tag on the ASP side
protected void gridViewSorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = GVInactive.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
string SQL = "[" + e.SortExpression + "] " + ConvertSortDirectionToSql(e.SortDirection);
dataView.Sort = SQL;
GVInactive.DataSource = dataView.ToTable();
GVInactive.DataBind();
}
}
private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "DESC";
break;
case SortDirection.Descending:
newSortDirection = "ASC";
break;
}
return newSortDirection;
}
However, the second time I click on the header it's supposed to reverse the previous sorting order. It never does. Every time I click the header of a column, it hits the case SortDirection.Ascending: line and sets newSortDirection = "DESC". The data is sorted in descending order, yet when I click the header again it resolves SortDirection to Ascending.
Any ideas?
We use a ViewState variable to store the latest Sort Direction. When the grid is sorted we compare the Sort Criteria and Sort Direction of the grid with the ViewState variables which stores last sort expression. If the columns are equal then check the direction of the previous sort and sort in the opposite direction.
Example:
private string SortCriteria
{
get
{
if (ViewState["sortCriteria"] == null)
{
ViewState["sortCriteria"] = "";
}
return ViewState["sortCriteria"].ToString();
}
set
{
ViewState["sortCriteria"] = value;
}
}
private string SortDirection
{
get
{
if (ViewState["sortDirection"] == null)
{
ViewState["sortDirection"] = "";
}
return ViewState["sortDirection"].ToString();
}
set
{
ViewState["sortDirection"] = value;
}
}
protected void gvData_Sorting(object sender, GridViewSortEventArgs e)
{
gvData.EditIndex = -1;
if (SortCriteria == e.SortExpression)
{
if (SortDirection == string.Empty || SortDirection == "DESC") { SortDirection = "ASC"; }
else { SortDirection = "DESC"; }
}
else
{
SortCriteria = e.SortExpression;
SortDirection = "ASC";
}
BindGrid();
}
private void BindGrid()
{
DataTable dt = new [However you get dataset from database];
DataView dv = new DataView(dt);
dv.Sort = string.Format("{0} {1}", SortCriteria, SortDirection).Trim();
gvData.DataSource = dv;
gvData.DataBind();
}
For the record, I replaced the code used in my question with this, and it worked perfectly:
// gridViewSorting and ConvertSortDirectionToSql are both necessary to ensure the gridview can sort when their column headers are
// clicked. You must remember to add (AllowSorting="True" OnSorting="gridViewSorting") to the gridview tag on the ASP side
protected void gridViewSorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = GVInactive.DataSource as DataTable;
string sortExpression = e.SortExpression;
string direction = string.Empty;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
if (SortDirection == SortDirection.Ascending)
{
SortDirection = SortDirection.Descending;
direction = " DESC";
}
else
{
SortDirection = SortDirection.Ascending;
direction = " ASC";
}
DataTable table = GVInactive.DataSource as DataTable;
table.DefaultView.Sort = sortExpression + direction;
GVInactive.DataSource = table;
GVInactive.DataBind();
}
}
public SortDirection SortDirection
{
get
{
if (ViewState["SortDirection"] == null)
{
ViewState["SortDirection"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["SortDirection"];
}
set
{
ViewState["SortDirection"] = value;
}
}
I can probably remove a few lines (not sure I even need the DataView anymore), but it works perfectly as is. Just remember to add these pieces to the ASP side in the GridView tag:
AllowSorting="True" OnSorting="gridViewSorting"
and then change your gridview's name where appropriate.

GridView sorting ascending is not working

I have a GridView with sorting enabled. For a single page result both the ascending and descending works fine. But when there are multiple pages, descending alone works well. the asceding also works but when i click on the next pages, it becomes unsorted again.
I don't know whether the problem is because of the sort direction or paging. Kindly Help.
Below are the codes:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
GridView grid = sender as GridView;
//Retrieve the table from the session object.
DataTable dt = Session["List"] as DataTable;
if (dt != null)
{
//Sort the data.
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
grid.DataSource = Session["List"];
grid.DataBind();
}
}
private string GetSortDirection(string column)
{
string sortDirection = "ASC";
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
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;
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = bindGridView();
GridView1.DataBind();
}
You can use a session variable to store the latest Sort Expression and when you sort the grid next time compare the sort expression of the grid with the Session variable which stores last sort expression. If the columns are equal then check the direction of the previous sort and sort in the opposite direction.
DataTable sourceTable = GridAttendence.DataSource as DataTable;
DataView view = new DataView(sourceTable);
string[] sortData = Session["sortExpression"].ToString().Trim().Split(' ');
if (e.SortExpression == sortData[0])
{
if (sortData[1] == "ASC")
{
view.Sort = e.SortExpression + " " + "DESC";
this.ViewState["sortExpression"] = e.SortExpression + " " + "DESC";
}
else
{
view.Sort = e.SortExpression + " " + "ASC";
this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";
}
}
else
{
view.Sort = e.SortExpression + " " + "ASC";
this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";
}
After so many research, I found the correct answer for my own question. This would probably help someone in future. Add the following code to the bindGridView()
if (ViewState["sortExpr"] != null)
{
dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["sortExpr"];
}
else
dv = ds.Tables[0].DefaultView;
and
#region Sorting
protected void Gridview1_Sort(object sender, GridViewSortEventArgs e)
{
ViewState["sortExpr"] = e.SortExpression + " " + GetSortDirection(e.SortExpression);
Gridview1.DataSource = bindGridView();
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;
}
#endregion

Gridview sorting in ASP.NET

I have a gridview and its data source comes from linq to sql statements:
var query = from user in dataContext.tbl_files
select new { user.File_Name, user.Upload_Time, user.Uploaded_By };
GridView1.DataSource = query.ToList();
GridView1.DataBind();
I am trying to implement sorting feature of gridview :
public void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string previousSortExpression = (string)ViewState["SortDirection"];
string sortExpression = e.SortExpression;
SortDirection sortDirection = e.SortDirection;
if (sortExpression.Equals(previousSortExpression))
{
sortDirection = SortDirection.Descending;
ViewState["SortDirection"] = string.Empty;
}
else
ViewState["SortDirection"] = sortExpression;
string direction = sortDirection == SortDirection.Ascending ? "ASC" : "DESC";
e.SortExpression = string.Format("it.{0} {1}", e.SortExpression, direction);
DataTable dataTable = (DataTable)GridView1.DataSource; // here returns null!
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression ;
GridView1.DataSource = dataView;
GridView1.DataBind();
}
}
But " DataTable dataTable = (DataTable)GridView1.DataSource; " line returns null however count of datasource is 4. I got this error :
{"Unable to cast object of type 'System.Collections.Generic.List1[<>f__AnonymousType03[System.String,System.Nullable`1[System.DateTime],System.String]]' to type 'System.Data.DataTable'."}
How can I sort my gridview and correct my mistake? Thanks..
You can't use the DataSource property of the grid to fetch the data source. It is only available after it is set and to the end of that postback. You'll need the fetch the data from the database again.
Something like this:
public void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
var users = (from user in dataContext.tbl_files
select new { user.File_Name, user.Upload_Time, user.Uploaded_By }).ToList().AsEnumerable();
switch(e.SortExpression)
{
case "File_Name":
users = users.OrderBy(x => x.File_Name);
break;
case "Upload_Time":
users = users.OrderBy(x => x.Upload_Time);
break;
case "Uploaded_By":
users = users.OrderBy(x => x.Uploaded_By);
break;
}
if(e.SortDirection == SortDirection.Descending)
users = users.Reverse();
GridView1.DataSource = users;
GridView1.DataBind();
}
Above answer is correct first you need to assign datasource to gridview again as sorting event is called.
So have to store the data somewhere.
For retrieving the datasource from grid you can follow below steps
The problem lies as you are assigning linq result as datasource of gridview and then taking datatable from the gridview datasource.
Try this code
BindingSource bs = (BindingSource )Gv.DataSource;
DataTable Dt = (DataTable ) bs.DataSource;
Contact if you have any doubt

How to sorting the gridview when i click the header text on gridview in asp.net

I have developed an asp.net application that has a Gridview control. I want it to sort when I click on the Gridview header text in asp.net. Entity framework is used to bind the gridview.
protected void grdmortgagesaver_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}
private void SortGridView(string sortExpression, string direction)
{
// You can cache the DataTable for improving performance
var databind = from i in mortgageentites.Clients orderby i.LastName select i;
if (databind.Count() > 0)
{
grdmortgagesaver.DataSource = databind.ToList();
grdmortgagesaver.DataBind();
DataTable dt = clients;
if (dt != null)
{
// DataTable dt = ds.Tables[0];
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
grdmortgagesaver.DataSource = dv;
grdmortgagesaver.DataBind();
}
}
}
If you are using a standard ASP.NET System.Web.UI.WebControls.GridView, you can simply "Enable Sorting", in the properties pane by setting "AllowSorting" to true or via the "pop-out" on the top right of the control by checking "Enable Sorting"
add AllowSorting="true" in your gridview control and
protected void grd_Sorting(object sender, GridViewSortEventArgs e)
{
if (e.SortExpression == hdfSortExp.Value)
{
if (hdfUpDown.Value == "1")
hdfUpDown.Value = "0";
else
hdfUpDown.Value = "1";
}
else //New Column clicked so the default sort direction will be incorporated
hdfUpDown.Value = "0";
hdfSortExp.Value = e.SortExpression; //Update the sort column
BindGrid(hdfSortExp.Value, this.CBool(hdfUpDown.Value));
}
in your c# code
public void BindGrid(string sortBy, bool inAsc)
{
grd.DataSource = WManager.GetAdminTags(txtSearch.Text.Trim(), sortBy, inAsc);
grd.DataBind();
}
First in your Page in the GridView Declaration Tag add the property AllowSorting="true" and in the Gridview_SortCommand try the Following code as guidence
try {
if (SortType == false) {
SortView.Sort = e.SortExpression + " ASC";
//dviewGeneral.Sort = e.SortExpression & " ASC"
Session.Item("SortOrder") = SortView.Sort.ToString;
SortType = true;
} else {
SortView.Sort = e.SortExpression + " DESC";
Session.Item("SortOrder") = SortView.Sort.ToString;
//dviewGeneral.Sort = e.SortExpression & " DESC"
SortType = false;
}
CMSgrid.SelectedIndex = -1;
CMSgrid.CurrentPageIndex = 0;
SortBind();
} catch (Exception ex) {
UserMsgBox(ex.ToString());
}

Categories

Resources