[英]Sorting gridview column in asp.net
我有一個從數據庫加載數據的gridview。 數據第一次顯示在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;
}
我沒有任何錯誤,我也不知道我的錯誤在哪里,有人可以幫我解決這個問題嗎?
提前致謝。
我找到了解決方案。 希望這會幫助其他面臨類似問題的人。 在.aspx頁面上需要更改:
來自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;
}
}
簡單,將Allowsorting屬性設置為true並在gridview中添加排序事件。
<asp:GridView ID="GridView1" runat="server" AllowSorting="true" OnSorting="GridView1_Sorting"> </asp:GridView>
現在在.cs文件中添加Event OnSorting
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"; } }
您必須使用缺少排序表達式的這些代碼
<asp:DataGrid ID="Datagrid1" runat="server" AllowPaging="True" AllowSorting="True" OnSorting="ComponentGridView_Sorting">
<Columns>
<asp:BoundColumn DataField="main_post" HeaderText="Header Post ID" SortExpression="main_post" />
</Columns>
</asp:DataGrid>
希望對您有幫助。
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="empid" DataSourceID="SqlDataSource1" EnableModelValidation="True" ForeColor="#333333" GridLines="None">
不要讓緊張的開發人員跟着我的腳步走,真的太容易了
步驟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 Yadav Vaibhav Designs http://vaibhavdesigns.org了解更多...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.