如何从datagridview中的复选框选定行获取单元格值 [英] how to get cell value from checkbox selected row in datagridview

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

问题描述

我使用下面的代码来获取使用复选框从哪个行中选择的单元格值。我的价值空虚。







.aspx代码



i used below code to get cell value from which row selected using checkbox. i am getting empty value.



.aspx code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"

            CellPadding="4" onrowdeleting="GridView1_RowDeleting">

    <Columns>

    <asp:TemplateField>
    <HeaderTemplate>ID</HeaderTemplate>
    <ItemTemplate>
        <asp:Label ID="lblid" runat="server" Text='<%#Bind("id")%>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField>
    <HeaderTemplate>Email-ID</HeaderTemplate>
    <ItemTemplate>
        <asp:Label ID="lblemail" runat="server" Text='<%#Bind("emailid")%>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField>
    <HeaderTemplate>fbsubject</HeaderTemplate>
    <ItemTemplate>
        <asp:Label ID="lblsubject" runat="server" Text='<%#Bind("fbsubject")%>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField>
    <HeaderTemplate>comments</HeaderTemplate>
    <ItemTemplate>
        <asp:Label ID="lblcomments" runat="server" Text='<%#Bind("comments")%>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField>

            <HeaderTemplate>Operation</HeaderTemplate>

            <ItemTemplate>

            <asp:Button ID="btnDelete" runat="server" CommandName="Delete" Text="Delete" CausesValidation="true"/>

            </ItemTemplate>
            </asp:TemplateField>

          <asp:TemplateField>
          <HeaderTemplate> Send Mail </HeaderTemplate>
          <ItemTemplate>
              <asp:CheckBox ID="chkmail" runat="server" />
          </ItemTemplate>
          </asp:TemplateField>

    </Columns>
    </asp:GridView>



c #code


c#code

try
        {
            ArrayList names = new ArrayList();
            foreach (GridViewRow gvr in this.GridView1.Rows)
            {
                if (((CheckBox)gvr.FindControl("chkmail")).Checked == true)
                {
                    names.Add(gvr.Cells[1].Text);
                }
            }

            //this.Label1.Text = string.Empty;
            foreach (object itm in names)
            {
                this.Label1.Text += " " + itm.ToString();

            }

        }
        catch (SqlException err)
        {
            Response.Write(err.Message);
        }

推荐答案

试试这个:

Try this:
foreach (GridViewRow row in GridView1.Rows)
   {
       CheckBox chk = (CheckBox)row.FindControl("Yourcheckboxname");
       if(chk.Checked)
      {
               //access values as follows
              string strDummy= ((Label)row.FindControl("yourcolumnname")).Text;
              // like this access the remaining controls that you need
              // your code to save the data
      }
   }





参考以下主题:

维护GridView内不同页面中选定CheckBox的状态 [ ^ ]



替代方案:



Refer following thread:
Maintaining States of Selected CheckBoxes in Different Pages inside the GridView[^]

Alternative:

protected void Button2_Click(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count > 0)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                //finding checkbox in GridView
                CheckBox cbx = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                //CheckBox not null
                if (cbx != null)
                {
                    //if CheckBox Checked
                    if (cbx.Checked)
                    {
                        //add your logic here.
                        //this is for example to get first column values of selected CheckBox
                        string firstColumnValue = GridView1.Rows[i].Cells[0].Text;
                        Response.Write(firstColumnValue);
                    }
                }
            }
        }
    }


试试这个:

Try this:
foreach (GridViewRow r in GridView1.Rows )
            {
                CheckBox ctl= (CheckBox)r.FindControl("chkmail");
                if (ctl.Checked)
                {
                    Label l = (Label)r.FindControl("lblid");
                    string i = l.Text;
                }
            }


这篇关于如何从datagridview中的复选框选定行获取单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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