GridView控件修改、删除、分页、排序示例(修改含有DropDownList控件)

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnSorting="GridView1_Sorting" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnPageIndexChanging="GridView1_PageIndexChanging" CellPadding="4" AllowSorting="True" AllowPaging="True" PageSize="18">
    <Columns>
     <asp:BoundField DataField="class_id" HeaderText="编号" ReadOnly="True" SortExpression="class_id">
      <ItemStyle Width="60px" />
     </asp:BoundField>
     <asp:BoundField DataField="class_name" HeaderText="类别名称" SortExpression="class_name">
      <ItemStyle Width="200px" />
     </asp:BoundField>

     <asp:TemplateField HeaderText="父类别" SortExpression="class_parent">
      <ItemTemplate>
       <%#bindParent_Name(Eval("class_parent").ToString())%>
      </ItemTemplate>
      <EditItemTemplate>
       <asp:HiddenField ID="HDFXueli" runat="server" Value='<%# Eval("class_parent") %>' />
       <asp:DropDownList ID="DDLXueli" runat="server" Width="90px" />
      </EditItemTemplate>
      <ItemStyle Width="100px" />
     </asp:TemplateField>

     <asp:CommandField EditText="修改" HeaderText="修改" ShowEditButton="True" >
      <ItemStyle Width="60px" />
     </asp:CommandField>
     <asp:CommandField HeaderText="删除" ShowDeleteButton="True" >
      <ItemStyle Width="60px" />
     </asp:CommandField>
    </Columns>
    <PagerStyle HorizontalAlign="Center" Font-Bold="True" />
    <HeaderStyle HorizontalAlign="Left" />
    <SelectedRowStyle Font-Bold="True" />
    </asp:GridView> private void Bind()
 ...{
  string key = "";
  string parent_id = "";
   try
  ...{
   key = Request.QueryString["key"];
   parent_id = Request.QueryString["parent_id"];
  }
  catch
   ...{
   key = "";
    parent_id = "";
  }

  DataTable dt;
  if (parent_id != "" && parent_id != null)
  ...{
   Data d;
   string cond = "";
   if (key == "" || key == null)
   ...{
     if (parent_id == "0")
      cond = "class_parent!=0";
     else
      cond = "class_parent=" + parent_id;
   }
   else
   ...{
    if (parent_id == "0")
     cond = "class_name like '%" + key + "%' and class_parent!=0";
    else
     cond = "class_name like '%" + key + "%' and class_parent=" + parent_id;
   }
   d = new Data("bookClass", cond, "class_id desc", 1);
   dt = d.getDataTable();
  }
  else
...{
dt = Dol.dtClassTwo();
}

  string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
  bind.bindGridView(this.GridView1, dt, "class_id", sort);
 }

 //根据父目录编号返回名称净化工程
  public string bindParent_Name(string class_id)
  ...{
   string parent_name = class_id;
    for (int i = 0; i < dt.Rows.Count; i++)
   ...{
     if (dt.Rows[i][0].ToString().Trim() == class_id.Trim())
    ...{
     parent_name = dt.Rows[i][1].ToString();
     break;
}
  }
  return parent_name;
 }

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 ...{
  if (((DropDownList)e.Row.FindControl("DDLXueli")) != null)
  ...{
   DropDownList ddlxueli = (DropDownList)e.Row.FindControl("DDLXueli");

   ddlxueli.DataSource = dt;
   ddlxueli.DataTextField = "class_name";
   ddlxueli.DataValueField = "class_id";
   ddlxueli.DataBind();

   // 选中 DropDownList
   ddlxueli.SelectedValue = ((HiddenField)e.Row.FindControl("HDFXueli")).Value;
  }

  //如果是绑定数据行净化设备
  if (e.Row.RowType == DataControlRowType.DataRow)
  ...{
   if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
   ...{
     ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:[" + e.Row.Cells[1].Text + "]吗?\n删除类别[" + e.Row.Cells[1].Text + "]后该类别下的所有图书、子目录和子目录下的图书都将被删除!')");
   }
  }
 }

 //排序
 protected void GridView1_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();
 }

 //编辑
 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
 ...{
  GridView1.EditIndex = e.NewEditIndex;
  Bind();
 }

 //删除
 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
 ...{
  string class_id = GridView1.DataKeys[e.RowIndex].Value.ToString();
  //删除该 目录下的图书
  DataDel d1 = new DataDel("book", "class_id=" + class_id);
  d1.ExceDel();

  //删除该目录下的子目录
  DataDel d3 = new DataDel("bookClass", "class_parent=" + class_id);
  d3.ExceDel();

  //删除该目录
  DataDel d = new DataDel("bookClass", "class_id=" + class_id);
  d.ExceDel();

  Bind();
 }

 //更新
 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 ...{
  string class_name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
  string class_id = GridView1.DataKeys[e.RowIndex].Value.ToString();
  string class_parent = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLXueli")).SelectedValue;

  Data d1 = new Data();
  if (d1.count("bookClass", "class_name='" + class_name + "' and class_parent=" + class_parent + " and class_id!=" + class_id) == 0)
  ...{
   DataUpdate d = new DataUpdate("bookClass", "class_name='" + class_name + "',class_parent=" + class_parent, "class_id=" + class_id);
   d.ExceUpdate();
   GridView1.EditIndex = -1;
   Bind();
  }
  else
  ...{
   Alert a = new Alert("[" + class_name + "]名称重复!");
   a.show();
  }
 }

 //取消
 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
 ...{
  GridView1.EditIndex = -1;
  Bind();
 }

 //分页
 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
 ...{
   GridView1.PageIndex = e.NewPageIndex;
  Bind();
 }