自个写的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();
     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值