我用gridview绑定了一张表,用模板的形式绑定的,现在我想要实现点击每行最后的按钮后就把此行添加到另外一张表中,
就是要先取出当前行的所有值后再插入到库中。要怎么实现啊, 请大家帮帮忙啊谢谢

解决方案 »

  1.   


    //假设你每行最后的按钮 是linkbutton:
    protected void LinkButton1_Click(object sender, EventArgs e)
            {
                GridViewRow row = (sender as LinkButton).Parent.Parent as GridViewRow;
                string id = row.Cells[0].Text;
                //以此类推,得到第列的值
               
            }
      

  2.   

    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "save_btn")   //如果是OpenRow按纽
            {
                //string sql;
                int iIndex = Convert.ToInt32(e.CommandArgument);
                string nID = this.GridView2.DataKeys[iIndex].Value.ToString();
                strsql88 = "select * from stone_out where storage_code='" + nID + "'";
               /* SqlCommand sqlcom = new SqlCommand(sql, con);
                con.Open();
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = sqlcom;
                DataSet ds = new DataSet();
                sda.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
                con.Close();*/
                GridView1.EditIndex = -1;
                Databind88(strsql88);
            }
            if (e.CommandName == "save_btn1")
            {
                string sql;
                int iIndex = Convert.ToInt32(e.CommandArgument);
                string nID = this.GridView2.DataKeys[iIndex].Value.ToString();
                sql = "update stone_storage set if_finish='Y' where storage_code = '" + nID + "'";
                con.Open();
                SqlCommand com = new SqlCommand(sql, con);
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = com;
                DataSet ds = new DataSet();
                sda.Fill(ds);
                con.Close();
                //Response.Redirect(Request.Url.ToString());
                //GridView1.EditIndex = -1;
                //Databind88(strsql88);            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
                Response.Write("<Script>alert('结项完成!')</Script>");
                Response.Redirect(Request.Url.ToString());
            }
        }
      

  3.   

    帖出你前台gridview的代码来看看和后台click事件
      

  4.   

    <asp:GridView ID="GridView1" runat="server" Style="position: static;" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" Height="160px" Width="881px" BackColor="White" BorderColor="White" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal" >
                <Columns>
    <asp:HyperLinkField DataNavigateUrlFields="商品编号" DataNavigateUrlFormatString="商品详细信息.aspx?id={0}"
                         DataTextField="商品名" HeaderText="商品名称">
                         <ControlStyle CssClass="csl" />
                     </asp:HyperLinkField>
    <asp:TemplateField HeaderText="购买价格" SortExpression="购买价格">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("购买价格") %>' ></asp:TextBox>
                        </EditItemTemplate>
                        <ItemStyle  Width="100px" CssClass="cs2" />
                        <ItemTemplate>
                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                            <asp:Label ID="Label8" runat="server" Text='<%# Bind("购买价格") %>' Font-Size="12px"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
    <asp:TemplateField HeaderText="购买数量" SortExpression="购买数量">
                    <ItemStyle CssClass="cs2" />
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox5" runat="server"  Text='<%# Bind("购买数量") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server"  Width="30px" Height="20px"  Text='<%# Bind("购买数量") %>'></asp:TextBox>
                            
                        </ItemTemplate>
                    </asp:TemplateField>
    <asp:TemplateField HeaderText="操作" ShowHeader="False" >
                        <ItemTemplate>
                            &nbsp;<asp:Button ID="move" runat="server"  Text="移入收藏夹" OnClick="move_click" /><br />
                                                   <asp:LinkButton ID="LinkButton1" runat="server" Font-Size="12px" CausesValidation="False" Font-Underline="false" CommandName="Delete" 
                                Text="删除"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>后台
    protected void move_click(object sender, EventArgs e)
        {
            GridViewRow row = (sender as Button).Parent.Parent as GridViewRow;
            string id = row.Cells[1].Text;        Response.Write("<script>alert(" + id + ")</script>");
    }
    后台是先按照你说的那张方法取出第某列的值,但显示的是为空
      

  5.   

     string id = row.Cells[1].Text;你第二列“购买价格”是模板列 当然取不到值了string price=(row.FindControl("TextBox8") as TextBox).Text;
      

  6.   

    这个可以实现,那个72绝技里面就有,
    可以仔细研究研究,以后用Gridview是很有帮助的。
    http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx