惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
Martin Fowler
Martin Fowler
博客园_首页
量子位
T
Tailwind CSS Blog
博客园 - Franky
G
Google Developers Blog
D
DataBreaches.Net
Vercel News
Vercel News
B
Blog
Recent Announcements
Recent Announcements
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
爱范儿
爱范儿
博客园 - 【当耐特】
The Cloudflare Blog
H
Help Net Security
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
C
Check Point Blog
有赞技术团队
有赞技术团队
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 大牛博客

广东早茶的悠闲时光 用户中心 - 博客园 asp.net 常用字符串过滤方法 非常实用的Asp.net常用的51个代码 - 大牛博客 - 博客园 JS 中面向对象的5钟写法 GridView的RowCommand事件中取得行索引 - 大牛博客 - 博客园 .netframework3.5 中TimeZoneInfo 类的使用 GridView,DataList,Repeater,DetailsView的遍历行代码 - 大牛博客 - 博客园 ASP.NET 2.0上传图片生成缩略图类 合理选择贪婪模式与非贪婪模式 - 大牛博客 - 博客园 揭开正则表达式的神秘面纱 asp.net下载文件 - 大牛博客 - 博客园 C#数据结构之双向链表 C#数据结构之队列 ASP.NET四种页面导航方式之比较与选择 asp.net页面事件执行顺序 中英文字符串截取方法 - 大牛博客 - 博客园 ASP.NET纯数字验证码 SQL 2005 ROW_NUMBER() 语句分页 | SQL效率最高的分页查询数据
GridView数据绑定控件和ObjectDataSource数据源控件实现排序...
大牛博客 · 2010-08-17 · via 博客园 - 大牛博客

<asp:GridView ID="gvMain" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="Black" GridLines="Vertical"
         DataSourceID="odsUsers" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" Width="777px" DataKeyNames="Id" AllowPaging="True" PageSize="5" AllowSorting="True" OnSorting="gvMain_Sorting">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id"
                    Visible="False" />
                <asp:BoundField DataField="LoginId" HeaderText="LoginId" ReadOnly="True" SortExpression="LoginId" />
                <asp:BoundField DataField="LoginPwd" HeaderText="LoginPwd" SortExpression="LoginPwd"
                    Visible="False" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
                <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                <asp:BoundField DataField="Mail" HeaderText="Mail" SortExpression="Mail" />
                <asp:BoundField DataField="UserState" HeaderText="UserState" SortExpression="UserState"
                    Visible="False" />
                <asp:BoundField DataField="UserRole" HeaderText="UserRole" SortExpression="UserRole"
                    Visible="False" />
            </Columns>
            <FooterStyle BackColor="#CCCC99" />
            <RowStyle BackColor="#F7F7DE" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:ObjectDataSource ID="odsUsers" runat="server" SelectMethod="GetAllUsersSort"
            TypeName="MyBookShop.BLL.UserManager" UpdateMethod="ModifyPartUser" DataObjectTypeName="MyBookShop.Models.User">          
            <SelectParameters>
                <asp:Parameter DefaultValue="LoginId" Name="sort" Type="String" />
            </SelectParameters>
        </asp:ObjectDataSource>

protected void gvMain_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["sort"] == null)
            ViewState["sort"] = "";
        if (ViewState["sort"].ToString() == e.SortExpression)
        {
            odsUsers.SelectParameters["sort"].DefaultValue = e.SortExpression + " desc";
            ViewState["sort"] = e.SortExpression + " desc";
        }
        else
        {
            odsUsers.SelectParameters["sort"].DefaultValue = e.SortExpression;
            ViewState["sort"] = e.SortExpression;
        }
        gvMain.DataBind();
        e.Cancel = true; //设置指示是否应取消事件的值。
    }

 public static IList<User> GetAllUsersSort(string sort)
{
            string sql = "SELECT * FROM users order by " + sort;
            return GetUsersBySql(sql);
}

private static IList<User> GetUsersBySql( string safeSql )
        {
            List<User> list = new List<User>();

   try
   {
    DataTable table = DBHelper.GetDataSet( safeSql );
    
    foreach (DataRow row in table.Rows)
    {
     User user = new User();
     
     user.Id = (int)row["Id"];
     user.LoginId = (string)row["LoginId"];
     user.LoginPwd = (string)row["LoginPwd"];
     user.Name = (string)row["Name"];
     user.Address = (string)row["Address"];
     user.Phone = (string)row["Phone"];
     user.Mail = (string)row["Mail"];
     user.UserState = UserStateService.GetUserStateById((int)row["UserStateId"]); //FK
     user.UserRole = UserRoleService.GetUserRoleById((int)row["UserRoleId"]); //FK
 
     list.Add(user);
    }
 
    return list;
   }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }

        }