在asp.net中排序gridview列
本文关键字:gridview 排序 asp net | 更新日期: 2023-09-27 18:10:20
我有一个gridview从数据库加载数据。数据第一次显示在gridview中,按datetime排序。在gridview的一列上,我想允许排序,例如在main_post列上。我已经尝试过了,但是当我点击列的标题时,gridview仍然没有排序。
这是gridview的前端代码:
<asp:DataGrid ID="Datagrid1" runat="server"
AllowPaging="True"
AllowSorting="True"
OnSorting="ComponentGridView_Sorting">
<Columns>
<asp:BoundColumn DataField="main_post" HeaderText="Header Post ID"
SortExpression="ComponentGridView_Sorting" />
</Columns>
</asp:DataGrid>
我的后端代码:
protected void loadData()
{
DataTable dtTemp;
dtTemp = objDBInterface.getResults(strSQL);
Datagrid1.DataSource = dtTemp;
Datagrid1.DataBind();
ViewState["dtbl"] = dtTemp;
}
protected void ComponentGridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = ViewState["dtbl"] as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);
Datagrid1.DataSource = dataView;
Datagrid1.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;
}
我没有任何错误,我不知道我的错误在哪里,有人可以帮助我解决这个问题吗?
简单,设置Allowsorting属性为true,并在gridview中添加排序事件。
<asp:GridView ID="GridView1" runat="server" AllowSorting="true"
OnSorting="GridView1_Sorting">
</asp:GridView>
现在在。cs文件中添加事件排序
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
SetSortDirection(SortDireaction);
if (dataTable != null)
{
dataTable.DefaultView.Sort = e.SortExpression + " " +_sortDirection;
GridView1.DataSource = dataTable;
GridView1.DataBind();
SortDireaction = _sortDirection;
}
}
protected void SetSortDirection(string sortDirection)
{
if (sortDirection == "ASC")
{
_sortDirection = "DESC";
}
else
{
_sortDirection = "ASC";
}
}
我找到解决办法了。希望这将帮助其他面临类似问题的人。在.aspx页面需要更改:
from OnSorting="ComponentGridView_Sorting"
到onsortcommand="ComponentGridView_Sorting"
然后将此代码放在aspx.cs(后端代码):
protected void Datagrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
{
string strSQL;
DataTable dt;
strSQL = "YOUR SELECT STATEMENT (SQL)";
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;
}
}
不要紧张开发人员真的很容易跟着我的步骤
步骤1创建GridView
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" class="w3-table-all"
OnPageIndexChanging="OnPageIndexChanging" PageSize="20" >
<Columns>
<asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="250px" DataField="sn_no" HeaderText="ID" />
<asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="name" HeaderText="Name" />
<asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="mo" HeaderText="Mobile" />
<asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="email" HeaderText="Email Id" />
<asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="loc" HeaderText="Location" />
<asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="date" HeaderText="Date" />
<asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="day2" HeaderText="Day" />
<asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="date" HeaderText="Date" />
<asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="time" HeaderText="Time" />
</Columns>
</asp:GridView>
步骤2进入*.aspx.cs文件,写入
)。降序
cmd = new MySqlCommand("SELECT * FROM appointment ORDER BY sn_no DESC", con);
b)。升序
cmd = new MySqlCommand("SELECT * FROM appointment ORDER BY sn_no ASC", con);
谢谢你的爱#Vaibhav YadavVaibhav设计http://vaibhavdesigns.org
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="empid" DataSourceID="SqlDataSource1" EnableModelValidation="True" ForeColor="#333333" GridLines="None">