请问我想在GirdView 中的第5列加一列Button控件 实现单击某一行的Button控件 可以更新本行第1列的文字内容 请问这行第1列的内容 怎么用语句表示?如(string A = 这行第1列的内容 )这种形式。

解决方案 »

  1.   

    i是行索引
    string A=GridView1.Rows[i].Cells[0].Text;
      

  2.   


     <asp:TemplateField HeaderText="点击">
          <ItemTemplate>
              <asp:Button ID="ButtonClick" runat="server" CommandArgument='<%# Eval("id") %>' CommandName="ClickButton" />
          </ItemTemplate>
    </asp:TemplateField>// id 是当前行的标识 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
     {
         if (null != e.CommandName)
         {
             string cmd = e.CommandName;
             int id = 0;
             if (null != e.CommandArgument)
             {
                 id = Convert.ToInt32(e.CommandArgument);
                 if ("ClickButton".Equals(cmd))
                 {
                      // 在这里面修改当前行第一列的值
                 }
             }
         }
    }
      

  3.   

    动态生成 ButtonFieldButtonField buttonF1 = new ButtonField();
    buttonF1.CommandName = "btn_Edit";
    buttonF1.Text = "编辑";
    buttonF1.HeaderText = "编辑";
    buttonF1.ButtonType = ButtonType.Button;
    GridView1.Columns.Add(buttonF1);然后 GridView1_RowCommand 事件就是按钮的事件了。这里你可以取得行号       protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName.Equals("btn_Delete"))
                {
                   //.......
                }
                if (e.CommandName.Equals("btn_Edit"))
                {
                //.......
                        int rowNum = GridView1.PageIndex * GridView1.PageSize + Convert.ToInt32(e.CommandArgument);    
    //Convert.ToInt32(e.CommandArgument)就是行号,这里是因为GridView1有分页,
    //没有直接取就行      
                }
            }
    有了行号,GridView1 就随你蹂躏了
      

  4.   

    GridView1.Rows[e.row].Cells[0].Text; e.你点一下就能看到,操作的是当前行
      

  5.   

    i 就是索引 e.Rowindex不用那个也行  直接在你的button里直接绑定 那一行的ID
    <asp:LinkButton ID="lkb_delete"  CommandName="Delete"   CommandArgument= ' <%#Eval( "id ") Text="删除"  %> '/> Gridview中的deleting事件
    在后台获取的时候
     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {            int id = Convert.ToInt32((this.GridView1.Rows[e.RowIndex].FindControl("lkb_delete") as LinkButton).CommandArgument);

      

  6.   

    <asp:LinkButton ID="lbtnUpdate" runat="server" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Update">修改</asp:LinkButton>
    <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name")%>'></asp:TextBox>//要修改的那列protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
            if (e.CommandName == "Update")
            {
                string txtName = null;
                txtName = ((TextBox)GridView1.Rows[Convert.ToInt32(e.CommandArgument.ToString())].FindControl("txtName")).Text.Trim();
    if ()
    {
                        //修改成功!
                        重新绑定到GridView
    }
    else
                        //修改失败!
    }
    }
      

  7.   

    起始用datalist不是更方便吗?你要的那效果