Solved

How to read gridview column and checkbox value.

Posted on 2014-03-10
3
3,761 Views
Last Modified: 2014-04-14
I have a grid view with binded data on it.
It has checkbox,verify and tid field in which verify field contains either 0 or 1, if verify field has 0 value row color is red and if 1 row color is green.
TID is list of unique numbers.

When if i click on checkbox and mark as check, the row color changes to green.
Now on submit button i want to read the tid number for every row where check box is checked.How to read that column and check whether checkbox is checked
as i want to update verify field at backend on click of submit button


The aspx of gridview is :
  <asp:GridView ID="GridView1" runat="server"   OnRowDataBound="GridView1_RowDataBound"
                    EnableModelValidation="True" ForeColor="#333333" GridLines="None"
                    BorderColor="#FFFFCC">
                 <Columns>
                <asp:TemplateField HeaderText="Select">
                <ItemTemplate>  
             
               
               <asp:CheckBox ID="myCheckBox" runat="server" />
                </ItemTemplate>
                 
                 
                </asp:TemplateField>
            </Columns>
              </asp:GridView>

code behind is:

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int ve = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Verify"));
            if (ve == 0)
            {
                e.Row.ForeColor = Color.Red;

            }
            else if (ve == 1)
            {
                e.Row.ForeColor = Color.Green;
             
            }
            else
            {
                e.Row.ForeColor = Color.Red;
            }

        }
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {

        foreach (GridViewRow row in GridView1.Rows)
        {

           
            if (((CheckBox)row.FindControl("cbm1")).Checked)
            {
               
 ?????????????????????
            }

        }
    }
0
Comment
Question by:searchsanjaysharma
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Jerry Miller earned 500 total points
ID: 39918382
This is how I access the checkbox values:

C# Version:
foreach (GridViewRow Item in gridview.Rows) {
      MyCheckBox = (CheckBox)Item.FindControl("cbName");
      if (MyCheckBox.Checked) {
           'do domething here
      }
             }

VB version:
For Each Item As GridViewRow In gridview.Rows
            MyCheckBox = CType(Item.FindControl("cbName"), CheckBox)
            If MyCheckBox.Checked Then
               
                    'do something here

                End If
            End If
        Next
0
 

Author Comment

by:searchsanjaysharma
ID: 39919502
How to refer to current row in Girdview that is the corresponding tid column of any row
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 39999676
tx
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question