GridView的使用-1

    <form id="form1" runat="server">
    <div>
     <table>
            <tr><td  style="height: 39px; color: white; background-color: #003399;text-align:center">商品信息管理</td></tr>
            <tr><td><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
            <Columns>
                <asp:TemplateField HeaderText="商品名称">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("GoodName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("GoodName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="产地">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtChanDi" runat="server" Text='<%# Bind("ChanDi") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("ChanDi") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="批号">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtPiHao" runat="server" Text='<%# Bind("PiHao") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("PiHao") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="供应商">
                    <EditItemTemplate>
                        <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("Company.Id") %>' />
                        <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Company.CompanyName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
            </Columns>
        </asp:GridView></td></tr>
        </table>
    </div>
    </form>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = new GoodManager().GetAllGood();
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataSource = new GoodManager().GetAllGood();
        GridView1.DataBind();
        //设置供应商列信息
        DropDownList ddl = GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1") as DropDownList;
        ddl.DataSource = new CompanyManager().GetAllCompanys();
        ddl.DataTextField = "CompanyName";//用于显示的字段
        ddl.DataValueField = "Id";//用于存值的字段
        ddl.DataBind();
        ddl.SelectedValue = (GridView1.Rows[e.NewEditIndex].FindControl("HiddenField1") as HiddenField).Value;
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Good gd = new Good();
        gd.Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        gd.GoodName = (GridView1.Rows[e.RowIndex].FindControl("txtName") as TextBox).Text;
        gd.ChanDi=(GridView1.Rows[e.RowIndex].FindControl("txtChanDi") as TextBox).Text;
        gd.PiHao=(GridView1.Rows[e.RowIndex].FindControl("txtPiHao") as TextBox).Text;
        int n = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList).SelectedValue);
        gd.Company = new CompanyManager().GetCompanyById(n);
        if (new GoodManager().ModifyGood(gd))
        { Response.Write("<script>alert('修改成功')</script>");
        GridView1.EditIndex = -1;
        GridView1.DataSource = new GoodManager().GetAllGood();
        GridView1.DataBind();
        }
        else
            Response.Write("<script>alert('修改失败')</script>");
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridView1.DataSource = new GoodManager().GetAllGood();
        GridView1.DataBind();
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值