自个写的GridView编辑并带dropdownlist(新手)

<asp:GridView ID="GridView1" runat="server"   AllowPaging="false"
             AutoGenerateColumns="False"   DataKeyNames="small_id" OnRowDataBound="GridView1_OnRowDataBound"   OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" Width="100%">
             <Columns>
             <asp:BoundField HeaderText="小类名称" DataField="small_class" />
             <asp:TemplateField HeaderText="大类名称">
            <EditItemTemplate>
            <asp:DropDownList runat="server" ID="ddlist"></asp:DropDownList>
            <asp:HiddenField ID="bigd" runat="server" Value='<%#Eval("big_id") %>' />
            </EditItemTemplate>
            <ItemTemplate>
                <%#Eval("big_class") %><asp:HiddenField ID="bigd" runat="server" Value='<%#Eval("big_id") %>' />
            </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField HeaderText="编辑" ShowEditButton="true" />
            <asp:TemplateField>
            <ItemTemplate>
           <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
                             Text="删除" OnClientClick="return confirm('确定要删除此记录吗?')"></asp:LinkButton>
            </ItemTemplate>
            </asp:TemplateField>
             </Columns>  
</asp:GridView>

#region  绑定GridView
public void db()
     {
         small_class sc = new small_class();
         DataSet ds = sc.get_big_small();
         this.GridView1.DataSource = sc.get_big_small();
         this.GridView1.DataBind();
     }
#endregion
#region  绑定GriVie中嵌套DropDownList数据
     protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
     {
         if ((DropDownList)e.Row.FindControl("ddlist") != null)
         {
             DropDownList dl = (DropDownList)e.Row.FindControl("ddlist");
             Big_class bc = new Big_class();
             dl.DataSource = bc.Get_big_class();
             dl.DataTextField = "big_class";
             dl.DataValueField = "big_id";
             dl.DataBind();
             if ((HiddenField)e.Row.FindControl("bigd") != null)
             {
                 HiddenField hd = (HiddenField)e.Row.FindControl("bigd");
                 dl.SelectedValue = hd.Value;
             }
         }
     }
#endreigon
     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
     {
         this.GridView1.EditIndex = e.NewEditIndex;
         this.db();
     }
     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
     {
         string strKey =GridView1.DataKeys[e.RowIndex].Values[0].ToString();
         string str = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
         string strk = (((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlist")).SelectedValue.ToString());
         small_class sc = new small_class();
         sc.Update_small(str, int.Parse(strk),int.Parse(strKey));
         this.GridView1.EditIndex = -1;
         this.db();
     }
     protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
     {
         this.GridView1.EditIndex = -1;
         this.db();
     }
     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
     {
         string strKey = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
         small_class bc = new small_class();
         bc.Delete_small(strKey);
         this.db();
     }

posted @ 2007-07-05 09:25  Campagnia Te  Views(945)  Comments(0Edit  收藏  举报
By Campagnia Te