我试图在Visual Studio 2008(C#)中对DataGrid列进行排序。我已经按照所有示例和搜索进行了操作,但它无效。
这就是我所拥有的:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ShopFloor.Components;
namespace ShopFloor
{
public partial class WebForm1 : System.Web.UI.Page
{
protected dsSF dsSF1;
protected System.Data.SqlClient.SqlDataAdapter daDtl;
protected System.Data.SqlClient.SqlDataAdapter daHdrUpTo;
protected System.Data.SqlClient.SqlDataAdapter daUpTo;
private DataView dv;
private dsSF DataSetSF
{
get
{
return (dsSF) Session["DataSetSF"];
}
set
{
Session["DataSetSF"] = value;
}
}
protected string SortColumn
{
get
{
object o = ViewState["SortColumn"];
if (o != null)
{
if ((string)o == "Item_Number")
{
ViewState["SortColumn"] = "Item_Number";
return "Item_Number";
}
return (string)o;
}
else
return "Item_Number";
}
set
{
ViewState["SortColumn"] = value;
}
}
protected bool SortAscend
{
get
{
object o = ViewState["SortAscend"];
if (o != null)
return (bool)o;
else
return true;
}
set
{
ViewState["SortAscend"] = value;
}
}
//==============================================================================
protected void Page_Load(object sender, System.EventArgs e)
{
ShowHideTOC();
if(!IsPostBack)
{
daHdrUpTo.SelectCommand.Parameters["@WC_Id"].Value = WorkCellId;
daHdrUpTo.SelectCommand.Parameters["@Required_Date"].Value = upDate;
daHdrUpTo.Fill(dsSF1, "SFD10011");
DataSetSF = dsSF1;
dgSF.DataBind();
DataTable dt = DataSetSF.Tables[“SFD10011”];
dv = dt.DefaultView;
Session["grid"] = dv;
}
else
{
dsSF1 = DataSetSF;
}
}
protected void dgSF_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
SortDataGrid(e.SortExpression);
}
private void SortDataGrid(string sort)
{
string currentSortColumn = SortColumn;
SortColumn = sort;
if (currentSortColumn == SortColumn)
SortAscend = !SortAscend;
else
SortAscend = true;
BindDataGrid();
}
private void BindDataGrid()
{
object o = Session["grid"];
if (o != null)
{
dv = (DataView)Session["grid"];
if (SortAscend)
dv.Sort = SortColumn + " " + "ASC"; // ASCENDING ORDER
else
dv.Sort = SortColumn + " " + "DESC"; // DESCENDING ORDER
Session["grid"] = dv;
dgSF.DataSource = dv;
}
else
dgSF.DataSource = null;
dgSF.DataBind();
}
}
}
我将列标题设置为模板,并在"排序表达式"中添加了字段。并设置我的DataGrid以允许排序。
无法弄清楚为什么不排序。任何帮助将不胜感激。
谢谢。