GridView默认按某列自动排序

xzf_fancy 2012-05-23 11:04:27
我的GridView已设置了标题点击可以排序的了,但我要实现第一次打开就根据某列,如时间,来排序。
我之前是在SQL里写好排序再绑定,但现在我的SQL语句因为需要二次利用,不能有ORDER BY,
所以我要找另一个方法实现GridView排序。
...全文
688 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzf_fancy 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

引用 3 楼 的回复:

HTML code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True" OnSorting="GridView1_Sort……
[/Quote]

提示不包含BindGridView的定义?少了什么?
Junny 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

引用 3 楼 的回复:

HTML code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True" OnSorting="GridView1_Sort……
[/Quote]

汗、就是鼠标点击 列头,有的控件是自带有这个的
xzf_fancy 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

引用 3 楼 的回复:

HTML code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True" OnSorting="GridView1_Sort……
[/Quote]

这个会影响我原来的点击标题自动排序的设置吗
Junny 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

HTML code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True" OnSorting="GridView1_Sorting">



C# co……
[/Quote]
从用户、性能来讲比较挺这个(默认降序Desc)
花痴 2012-05-23
  • 打赏
  • 举报
回复
使用动态sql
xzf_fancy 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

HTML code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True" OnSorting="GridView1_Sorting">



C# co……
[/Quote]

比如我是根据列aa排序,你这个语句aa在哪体现,我看不出来?
  • 打赏
  • 举报
回复
控件绑定数据表bindingSource.Sort = "列名";
xzf_fancy 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

绑定的时候,高级选项里有排序吧
还有为什么二次利用就不能有ORDER BY了,重新写一遍不可以么
[/Quote]

重写一遍不太可行,我的selectcommand每按一次都变,变的里面含有上次的SQL语句,所以每次重写去掉ORDER BY的话也不是说不可以,但就更复杂了。
xzf_fancy 2012-05-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

你那个Sql语句(排序)应该没有必要 重复使用吧;直接 存储过程就好了。。
[/Quote]

那个SQL语句被用来做子查询了
Junny 2012-05-23
  • 打赏
  • 举报
回复
你那个Sql语句(排序)应该没有必要 重复使用吧;直接 存储过程就好了。。
C____1988 2012-05-23
  • 打赏
  • 举报
回复

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True" OnSorting="GridView1_Sorting">



/// <summary>
/// GridView排序事件
/// </summary>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
// 从事件参数获取排序数据列
string sortExpression = e.SortExpression.ToString();

// 假定为排序方向为“顺序”
string sortDirection = "ASC";

// “ASC”与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改
if (sortExpression == this.GridView1.Attributes["SortExpression"])
{
//获得下一次的排序状态
sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
}

// 重新设定GridView排序数据列及排序方向
this.GridView1.Attributes["SortExpression"] = sortExpression;
this.GridView1.Attributes["SortDirection"] = sortDirection;

this.BindGridView();
}

gdreamlend 2012-05-23
  • 打赏
  • 举报
回复
绑定的时候,高级选项里有排序吧
还有为什么二次利用就不能有ORDER BY了,重新写一遍不可以么
cnfixit 2012-05-23
  • 打赏
  • 举报
回复

GridView1.Sort()?

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧