我想在用户点击列标题时按列对网格视图进行排序。在这里,用户可以单击任何列,并根据单击的列对网格视图进行排序。这是我的代码:
<asp:GridView ID="gvEmployeeStatus" runat="server" AutoGenerateColumns="false" AllowPaging="True" PageSize="10" AllowSorting="true" OnPageIndexChanging="gvEmployeeStatus_PageIndexChanging" OnSorting="gvEmployeeStatus_Sorting" >
protected void gvEmployeeStatus_Sorting(object sender, GridViewSortEventArgs e)
{
loginName = (String)(Session["LoginName"]);
dsLoginDetail = clsBLogic.TblLogin(loginName);
tblEmployeeNo = dsLoginDetail.Tables[0].Rows[0]["EmployeeNo"].ToString();
BindDataTogvEmployeeStatus(tblEmployeeNo);
DataTable dataTable = gvEmployeeStatus.DataSource as DataTable;
if(dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);
gvEmployeeStatus.DataSource = dataView;
gvEmployeeStatus.DataBind();
}
}
private string ConvertSortDirection(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
网格视图中的记录来自数据集。现在,当我运行代码时,没有任何反应。我在gvEmployeeStatus_Sorting事件上设置了断点,以检查它何时被触发。它没有被解雇。我该怎么排序记录!!
答案 0 :(得分:0)
正如你提到的排序没有被触发,我想你错过了SortExpression
<asp:TemplateField> or <asp:BoundField>
例如,
<asp:TemplateField HeaderText="Your Header 1" SortExpression="ColumName1">
或
<asp:BoundField DataField="Your Header 1" HeaderText="Your Header 1" SortExpression="ColumName1" />
答案 1 :(得分:0)
在aspx页面上将此属性设置为gridview。
onsortcommand="ComponentGridView_Sorting
。
然后将其放在aspx.cs页面的后端。
protected void Datagrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
{
string strSQL;
DataTable dt;
strSQL = "(SQL SELECT STATEMENT HERE)";
dt = strSQL;
{
string SortDir = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
SortDir = "Desc";
}
else
{
dir = SortDirection.Ascending;
SortDir = "Asc";
}
DataView sortedView = new DataView(dt);
sortedView.Sort = e.SortExpression + " " + SortDir;
Datagrid1.DataSource = sortedView;
Datagrid1.DataBind();
}
}
protected SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
答案 2 :(得分:0)
#region For Grid view Header Sorting..!!
public SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
protected void grdAdd_Sorting(object sender, GridViewSortEventArgs e)
{
string sortingDirection = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
sortingDirection = "Desc";
}
else
{
dir = SortDirection.Ascending;
sortingDirection = "Asc";
}
DataTable dtgrd = AdditionBL.BindAdditionMaster();/**Data Table Bind For Short View**/
DataView sortedView = new DataView(dtgrd);
sortedView.Sort = e.SortExpression + " " + sortingDirection;
grdAddition.DataSource = sortedView;
grdAddition.DataBind();
}
#endregion