zoukankan      html  css  js  c++  java
  • GridView 72般绝技之与Linq喜结连理 之2.GridView选中,编辑,取消,删除与3.GridView正反双向排序:

    源码在此篇中贴出来。

    园子里朋友的原文
    http://blog.csdn.net/lipan2/archive/2008/09/14/2795707.aspx
    2.GridView选中,编辑,取消,删除
      (1)设置GridView为可编辑,选择,删除的状态。然后绑定各自的事件

    代码

        
    <asp:GridView ID="gridViewCourse" runat="server" AutoGenerateColumns="False" 
            onrowcancelingedit
    ="gridViewCourse_RowCancelingEdit" 
            onrowdeleting
    ="gridViewCourse_RowDeleting" 
            onrowediting
    ="gridViewCourse_RowEditing" 
            onrowupdating
    ="gridViewCourse_RowUpdating" DataKeyNames="Sno">
            
    <Columns>
                
    <asp:BoundField DataField="Sno" HeaderText="学号" />
                
    <asp:BoundField DataField="Name" HeaderText="姓名" />
                
    <asp:BoundField DataField="Age" HeaderText="年龄" />
                
    <asp:BoundField DataField="BeginStudt" HeaderText="开学时间" />
                
    <asp:BoundField DataField="Road" HeaderText="路程" />
                
    <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                
    <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                
    <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
            
    </Columns>
        
    </asp:GridView>

    (2)然后是各自的处理事件:这段代码写的不太好,因为功能还没有实现。

    代码
          #region GridView 事件

            
    protected void gridViewCourse_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {
                
    if (studentService == null) studentService = new StudentService();
                
    string deleteCno=this.gridViewCourse.DataKeys[e.RowIndex].Value.ToString();
                Student deleteItem 
    = studentService.GetById(deleteCno);
                studentService.Delete(deleteItem);
                
    //this.gridViewCourse.DeleteRow(e.RowIndex);
                MessageBox.Show("删除成功");
                BindSource();
            }

            
    protected void gridViewCourse_RowEditing(object sender, GridViewEditEventArgs e)
            {
                
    if (studentService == null) studentService = new StudentService();
            }

            
    protected void gridViewCourse_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                
    if (studentService == null) studentService = new StudentService();
            }

            
    protected void gridViewCourse_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                
    if (studentService == null) studentService = new StudentService();
            }

            
    #endregion

            
    #region 绑定事件

            
    private void BindSource()
            {
                
    if (studentService == null) studentService = new StudentService();
                List
    <Student> courseList = studentService.FindAllList();
                
    this.gridViewCourse.DataSource = courseList;
                
    this.gridViewCourse.DataKeyNames = new string[] { "Sno" };
                
    this.gridViewCourse.DataBind();
            }

            
    #endregion

     3.GridView正反双向排序:
     设置GridView可排序,然后是要点击表头的链接才可以排序(AllowSorting="true"),所以选择排序的SortExpression;(SortExpression="Sno"/>)

    代码
              <asp:GridView ID="gridViewCourse" runat="server" AutoGenerateColumns="False" AllowSorting="true"
            DataKeyNames
    ="Sno" onsorting="gridViewCourse_Sorting">
             
    <FooterStyle BackColor="White" ForeColor="#000066" />
            
    <Columns>
                
    <asp:BoundField DataField="Sno" HeaderText="学号" SortExpression="Sno"/>
                
    <asp:BoundField DataField="Name" HeaderText="姓名"  SortExpression="Name"/>
                
    <asp:BoundField DataField="Age" HeaderText="年龄" SortExpression="Age"/>
                
    <asp:BoundField DataField="BeginStudt" HeaderText="开学时间" SortExpression="BeginStudt" />
                
    <asp:BoundField DataField="Road" HeaderText="路程" SortExpression="Road"/>
            
    </Columns>
            
    <RowStyle ForeColor="#000066" />
            
    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            
    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            
    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        
    </asp:GridView>      

     用ViewState保存排序字段,和排序规则。

    protected void gridViewCourse_Sorting(object sender, GridViewSortEventArgs e)
            {
                string sPage = e.SortExpression;
                if (ViewState["SortOrder"].ToString() == sPage)
                {
                    if (ViewState["OrderDire"].ToString() == "DESC")
                        ViewState["OrderDire"] = "ASC";
                    else
                        ViewState["OrderDire"] = "DESC";
                }
                else
                {
                    ViewState["SortOrder"] = e.SortExpression;
                }
                Bind();
            }

            private void Bind()
            {
                if (studentService == null) studentService = new StudentService();
                List<Student> courseList = studentService.FindAllList((string)ViewState["SortOrder"], (string)ViewState["OrderDire"] );
                this.gridViewCourse.DataSource = courseList;
                this.gridViewCourse.DataKeyNames = new string[] { "Sno" };
                this.gridViewCourse.DataBind();
            }

    代码:

    /Files/csharponworking/LineToSqlSample.rar

  • 相关阅读:
    hdu 1849 Rabbit and Grass(nim)
    sg函数模板
    hdu 1848 Fibonacci again and again(sg)
    hdu 1847 Good Luck in CET-4 Everybody!(sg)
    hdu 1846 Brave Game(bash)
    hdu 1517 A Multiplication Game(必胜态,必败态)
    hdu 1536/ hdu 1944 S-Nim(sg函数)
    hdu 2509 Be the Winner(anti nim)
    hdu 1907 John(anti nim)
    zoj 3965 Binary Tree Restoring(搜索)
  • 原文地址:https://www.cnblogs.com/csharponworking/p/1713090.html
Copyright © 2011-2022 走看看