获取的GridView中选定行 [英] Get the Selected row of Gridview

查看:107
本文介绍了获取的GridView中选定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对每一个row.By放置Gridview.I复选框选中此复选框并单击删除按钮我应该在$ C $的gridview的所有选定行cbehind.Please就帮我...

 < ASP:GridView控件ID =GridView1=服务器背景色=#DEBA84
        BORDERCOLOR =#DEBA84边框=无边框宽度=1px的CELLPADDING =3
        CELLSPACING =2的风格=保证金左:58px;的margin-top:13px的>
          < RowStyle的BackColor =#FFF7E7前景色=#8C4510/>
          <柱体和GT;
              < ASP:的TemplateField>
                  <&EditItemTemplate的GT;
                      < ASP:复选框ID =CheckBox1=服务器/>
                  < / EditItemTemplate中>
                  <&ItemTemplate中GT;
                      < ASP:复选框ID =CheckBox1=服务器/>
                  < / ItemTemplate中>
              < / ASP:的TemplateField>
          < /专栏>    < / ASP:GridView的>
    < ASP:按钮的ID =删除=服务器文本=按钮=服务器
        的onclick =Delete_Click/>

codebehind

 保护无效的button1_Click(对象发件人,EventArgs的发送)
    {
        字符串valueForDB = DatabaseList.SelectedValue;
        数据OBJ =新的Data();
        如果(valueForDB ==virtualworkplace)
        {
            obj.getDocforVirtualwrkspace(valueForDB);
            GridView1.DataSource = obj.getDocforVirtualwrkspace(valueForDB);
            GridView1.DataBind();
        }
什么写在这里?
 保护无效Delete_Click(对象发件人,EventArgs的发送)
    {
        }


解决方案

试试这个

 私人无效DeleteRows()
 {
        的foreach(在gridView1.Rows GridViewRow行)
        {
            HtmlInputCheckBox CHK =(HtmlInputCheckBox)row.Cells [0] .FindControl(selectedrowchk);
            如果(CHK = NULL&放大器;!&安培; chk.Checked)
            {
                字符串ID = gridView1.DataKeys [row.RowIndex] .Value.ToString(); //获得此行的记录的ID
                deleteRecord(ID);
            }
        }        // RefreshGrid();
    }

这code是假设你设置在GridView的DataKeyNames属性来存储每个记录的主键。它还假设你把复选框,在第一列

I have a Gridview.I placed checkbox on each row.By selecting the checkbox and clicking on delete button i should get all the selected row of that gridview in the codebehind.Please do help me...

<asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" 
        BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
        CellSpacing="2" style="margin-left: 58px; margin-top: 13px" >
          <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
          <Columns>
              <asp:TemplateField>
                  <EditItemTemplate>
                      <asp:CheckBox ID="CheckBox1" runat="server" />
                  </EditItemTemplate>
                  <ItemTemplate>
                      <asp:CheckBox ID="CheckBox1" runat="server" />
                  </ItemTemplate>
              </asp:TemplateField>
          </Columns>

    </asp:GridView>
    <asp:Button ID="Delete" runat="server" Text="Button"  runat="server" 
        onclick="Delete_Click" />

codebehind

protected void Button1_Click(object sender, EventArgs e)
    {
        string valueForDB = DatabaseList.SelectedValue;
        Data obj = new Data();
        if (valueForDB == "virtualworkplace")
        {
            obj.getDocforVirtualwrkspace(valueForDB);
            GridView1.DataSource = obj.getDocforVirtualwrkspace(valueForDB);
            GridView1.DataBind();
        }
What to write here???
 protected void Delete_Click(object sender, EventArgs e)
    {
        }

解决方案

Try this

 private void DeleteRows()
 {
        foreach (GridViewRow row in gridView1.Rows)
        {
            HtmlInputCheckBox chk = (HtmlInputCheckBox) row.Cells[0].FindControl("selectedrowchk");
            if (chk != null && chk.Checked)
            {
                string id = gridView1.DataKeys[row.RowIndex].Value.ToString(); // get the record's ID of this row
                deleteRecord(id);
            }
        }

        //RefreshGrid();
    }

This code is assuming you set the DataKeyNames property of the GridView to store the primary key of each record. It is also assuming you are putting the checkbox in the first column

这篇关于获取的GridView中选定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆