前台代码:

 
  
  1. <asp:GridView ID="GridView1" runat="server" Height="133px" Width="539px"   
  2.           AutoGenerateColumns="False" DataKeyNames="id"   
  3.           OnRowEditing="GridView1_RowEditing" BackColor="White" BorderColor="White"   
  4.           BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"  CellSpacing="1"   
  5.           GridLines="None" OnRowUpdating="GridView1_RowUpdating"   
  6.           OnRowCancelingEdit="GridView1_RowCancelingEdit"   
  7.           OnRowDataBound="GridView1_RowDataBound"  OnRowDeleting="GridView1_RowDeleting" >    
  8.       <Columns>    
  9.            <asp:BoundField DataField="channel" HeaderText="频道名称" />    
  10.          <asp:BoundField DataField="id" HeaderText="编号" DataFormatString="{0:000#}" ReadOnly="True" />    
  11.            <asp:BoundField DataField="descript" HeaderText="频道描述" />    
  12.           <asp:CommandField HeaderText="编辑" ShowEditButton="True" />    
  13.           <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />    
  14.        </Columns>    
  15.        <RowStyle BackColor="#DEDFDE" ForeColor="Black" />    
  16.        <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />    
  17.        <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />    
  18.        <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />    
  19.        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />    
  20.    </asp:GridView>  

后台代码:

 
  
  1. protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)  
  2.      {  
  3.          GridView1.EditIndex = e.NewEditIndex;  
  4.          BindDataList();  
  5.      }  
  6.  
  7.  
  8.      protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  9.      {  
  10.          this.GridView1.EditIndex = -1;  
  11.          BindDataList();  
  12.      }  
  13.  
  14.      protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  15.      {  
  16.          GridViewRow row = GridView1.Rows[e.RowIndex];  
  17.          string ID = GridView1.DataKeys[e.RowIndex].Values[0].ToString().Trim();  
  18.          NJ.Internet.NJ_Channel_Dal ncd = new NJ.Internet.NJ_Channel_Dal();  
  19.          int count = ncd.DelChannel(Convert.ToInt32(ID));  
  20.          if (count != 0)  
  21.          {  
  22.              Response.Write("<script type='text/javascript'>window.alert('删除成功')</script>");  
  23.              BindDataList();  
  24.          }  
  25.      }  
  26.  
  27.      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
  28.      {  
  29.  
  30.      }  
  31.  
  32.  
  33.      protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  34.      {  
  35.          try 
  36.          {  
  37.              GridViewRow row = GridView1.Rows[e.RowIndex];    
  38.              string Name = ((TextBox)(row.Cells[0].Controls[0])).Text;    
  39.              string Id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();  
  40.              string Descript = ((TextBox)(row.Cells[2].Controls[0])).Text.Trim();    
  41.              NJ.Internet.NJ_Channel_Dal ncd = new NJ.Internet.NJ_Channel_Dal();  
  42.              if (!String.IsNullOrEmpty(Id))  
  43.              {  
  44.                  int count = ncd.UptChannel(Convert.ToInt32(Id), Name, Descript);  
  45.                  if (count != 0)  
  46.                  {  
  47.                      GridView1.EditIndex = -1;    
  48.                      BindDataList();  
  49.                  }  
  50.              }  
  51.          }  
  52.          catch (Exception ee)  
  53.          {  
  54.              Response.Write("<scrit>alert('" + ee.Message + "')</script>");  
  55.          }  
  56.      } 

这实现了更新,删除还没有实现。希望大家能够参考,如果有错误请指出,大家共同进步