Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How do I get the value in the gridview which the checkbox is checked
in my gridview one column I created checkboxs ,when i click that checkbox of a row
a need to display the value of 3rd column of the checked row value on button click.
sample code I written is:

protected void Button_Click(object sender, EventArgs e)
   {

       foreach (GridViewRow itemrow in GridView1.Rows)
       {


          CheckBox cbview = (CheckBox)(itemrow.Cells[0].FindControl("viewdata"));

        if (cbview.Checked)
         {

            // String s = row.Cells[2].Text;
           string value = GridView1.DataKeys[itemrow.RowIndex]["id"].ToString();
         Response.Write(value);
}
}
}
Posted
Updated 15-Aug-11 23:02pm
v2
Comments
Sunasara Imdadhusen 16-Aug-11 5:05am    
What is your problem?
pradeep manne 16-Aug-11 5:14am    
am not getting the 3rd column value,i have changed the cell index value also
is there any other way to get the template column value og a grid

C#
protected void Button_Click(object sender, EventArgs e)
{
 foreach (GridViewRow itemrow in GridView1.Rows)
 {
   CheckBox cbview = (CheckBox)(itemrow.Cells[0].FindControl("viewdata"));
   if (cbview.Checked)
   {

      string value = GetObjectType(itemrow.Cells[2].Controls[1]);
      Response.Write(value);
   }
 }

private String GetObjectType(Control ctrl)
    {
        switch (ctrl.GetType().Name)
        {
            case "Label":
                return ((Label)ctrl).Text;
            case "TextBox":
                return ((TextBox)ctrl).Text;
        }
        return "";
    }
 
Share this answer
 
v2
Comments
pradeep manne 16-Aug-11 5:23am    
string value = itemrow.Cells[2].Text;
am getting null value in the variable
senthil sennu 16-Aug-11 5:26am    
Is that 3rd column is template field?
senthil sennu 16-Aug-11 5:29am    
I have update the solution. If it is template column u need to get the particular control text. I have return one method to return the Values. You can add you control in switch case if you need. Hope this helps.
pradeep manne 16-Aug-11 6:01am    
Thanks sennu its working
senthil sennu 16-Aug-11 6:04am    
Why you given low rating.Is the above solution dint solve your problem .Here rating 1 is low. 5 is high. mark the replies as answers if it really help. :)
take One hidden field with checkbox in that row
and store that value which you want to access in code behind

find that hidden field control in your code behind and...

SQL
if (cbview.Checked)
        {
              string value = hiddenfieldId.value;
        }
 
Share this answer
 
Hi,

See sample code:

Example:
In your Client Code you have to have a column TemplateField in your Grid...
<asp:TemplateField HeaderStyle-HorizontalAlign="Center"HeaderText="View">
<itemtemplate>
   <asp:LinkButton ID="viewdata" runat="server" Text="View"
       OnClick="viewdata_Click">
</itemtemplate>



In your code behind:

C#
protected void viewdata_Click(object sender, EventArgs e)
{
 LinkButton viewdata = sender as LinkButton;
 GridViewRow row = (GridViewRow)viewdata.NamingContainer;
 string column3 = row.Cells[2].Text.Trim();
}

In above example 3rd column or cell on your grid is ClaimNo
The variable column3 should be the value of ClaimNo because the column index begins at 0.


Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Hope this could help...

Regards,

Algem
 
Share this answer
 
v3

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