代码改变世界

GridView 72般绝技之与Linq喜结连理 之2.GridView选中,编辑,取消,删除与3.GridView正反双向排序:

2010-04-15 23:25  c#在路上  阅读(785)  评论(0编辑  收藏  举报

源码在此篇中贴出来。

 

园子里朋友的原文
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