2014年7月13日 星期日

[RESOLVED] Enable field sorting when gridview datasource from code behind


Ok, I'm not sure how to get this to work. I have a gridview and I've enabled paging and sorting on my gridview. I set the two fields in question to be sortable headers. Here is what I have so far:


 


// Populate GridView using data table from code behind

private void GetData()
{
DataTable table = new DataTable();
// get the connection
string _cs = DBUtils.DBString;
using (SqlConnection conn = new SqlConnection(_cs))
{
// write the sql statement to execute
string sql = "SELECT [userid], [department] FROM [Employees] order by userid";
// instantiate the command object to fire
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
// get the adapter object and attach the command object to it
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
// fire Fill method to fetch the data and fill into DataTable
ad.Fill(table);
}
}
}
GridView1.DataSource = table;
GridView1.DataBind();
}

// now for the code that is for sorting, I think this may be my problem.

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = GridView1.DataSource as DataTable;

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

GridView1.DataSource = dataView;
GridView1.DataBind();
}
}

// this code determines the selected sort order

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;
}



So, how do I get this sorting to work?



dataView. Sort = e. SortExpression + " " + ConvertSortDirectionToSql (e. SortDirection ); AFAIK, dv.sort= like : dataView. Sort = e. SortExpression + " Asc"

hi,


let the sorting work contains several steps:


1)set AllowSorting="True".


2)add SortExpression property in field.like:



3)define gridview sorting method.


Hope this helps



I'm using template fields not bound fields.



Ok, once I click the header, it sets the sort direction as ascending and retains that value no matter how many times I click on the header.



Hi,


You can use client side sorting mechanism. Refer the below urls which will use tablesorter & jquery for this because if we are doing it in server side we need to either fetch the data from db again or we need to keep it in session/other storage mechanism
for holding the data source of gridview won't be available on postback


http://dotnet-mcts.jecaestevez.com/2011/03/sort-aspnet-gridview-columns-using.html


http://www.iamraghuveer.com/2012/04/sorting-aspnet-gridview-control-using.html


Hope it helps




I finally figured this one out. Here's the link that helped me.


http://satindersinght.blogspot.com/2013/05/gridview-sorting-on-header-click-with.html


My relevant code sample:


 


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
List list = new List();
if (ViewState["SelectedRecords"] != null)
{
list = (List)ViewState["SelectedRecords"];
}
foreach (GridViewRow row in GridView1.Rows)
{
String id = (String)GridView1.DataKeys[row.RowIndex].Value;
CheckBox checkBox = (CheckBox)row.FindControl("chkSelect");
if (checkBox.Checked)
{
if (!list.Contains(id))
{
list.Add(id);
}
}
if (!checkBox.Checked)
{
list.Remove(id);
}
}
ViewState["SelectedRecords"] = list;
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = Session["objects"];
GridView1.DataBind();
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
GetData();
DataTable sourceTable = GridView1.DataSource as DataTable;
string sortingDirection = string.Empty;
if (direction == SortDirection.Ascending)
{
direction = SortDirection.Descending;
sortingDirection = "Desc";
}
else
{
direction = SortDirection.Ascending;
sortingDirection = "Asc";
}
DataView sortedView = new DataView(sourceTable);
sortedView.Sort = e.SortExpression + " " + sortingDirection;
Session["objects"] = sortedView;
DataView view = new DataView(sourceTable);
GridView1.DataSource = sortedView;
GridView1.DataBind();
}

public SortDirection direction
{
get
{
if (ViewState["directionState"] == null)
{
ViewState["directionState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["directionState"];
}
set
{ ViewState["directionState"] = value; }
}


沒有留言:

張貼留言