Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i used below code to get cell value from which row selected using checkbox. i am getting empty value.



.aspx code

XML
<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#
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);
        }
Posted
Updated 2-Jan-13 19:40pm
v3
Comments
[no name] 3-Jan-13 1:49am    
hi,

set debugger on this line "if (((CheckBox)gvr.FindControl("chkmail")).Checked == true)" & check whether this condition comes true or not.

Try this:
C#
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
      }
   }


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

Alternative:
C#
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);
                    }
                }
            }
        }
    }
 
Share this answer
 
v2
Comments
Umapathi K 3-Jan-13 2:32am    
i can get only fir row value other rows value unable to get
Prasad_Kulkarni 3-Jan-13 2:36am    
Please refer updated answer.
Try this:
C#
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;
                }
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900