Click here to Skip to main content
14,693,719 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi
how to get grid row values when checkbox is checked. i am using this code in button click event, but it's not working.. could you please advice.

foreach (GridViewRow rw in grid1.Rows)
      {
          CheckBox chkBx = (CheckBox)rw.FindControl("Check");
          if (chkBx != null && chkBx.Checked)
          {
            
              
              lblEmail = grid1.Rows[0].Cells[3].Text.ToString();
             

          }
      }

thanks in advance...
Posted
Comments
Shemeer NS 14-Jul-12 4:50am
   
It's not working means!!! getting any error? or not working as expected?
shashank raj gupta 27-Dec-12 12:32pm
   
if (RadioButtonList1.Text.Equals("Some"))
{
CheckBox chkDelete = (CheckBox)row.FindControl("CheckBox1");
//chkDelete.Checked = true;
if (chkDelete.Checked)
{
string id = row.Cells[1].Text;
con.Open();
cmd13 = new SqlCommand();
cmd13.Connection = con;
cmd13.CommandText = "insert into table1(class,date,select_id,attendence) values ('" + DropDownList1.Text + "','"+ TextBox1.Text + "','" + id + "','p')";
cmd13.ExecuteNonQuery();
con.Close();
Label1.Text = "lag gayi attendance";
}
else
{
string id = row.Cells[1].Text;
con.Open();
cmd13 = new SqlCommand();
cmd13.Connection = con;
cmd13.CommandText="insert into table1(class,date,select_id,attendence) values ('" + DropDownList1.Text + "','"+ TextBox1.Text + "','" + id + "','A')";
cmd13.ExecuteNonQuery();
con.Close();
Label1.Text = "lag gayi attendance";
}
i'm making a attendance project, i just want to do if i put some mark on chechbox then on those "a" should go to the database otherwise "p" should go if it is unmarked a=absent,p=present
shashank raj gupta 29-Dec-12 6:27am
   
it isn't working as expected.........i want that when i mark chechbox p should go be ...... to the database otherwise a should go when condition is equals to some.
shashank raj gupta 31-Dec-12 1:56am
   
plz help me out ....... fast

Hi,
As I think only one modification can find your solution.
Try this:
string strEmail=stirng.Empty;
foreach (GridViewRow rw in grid1.Rows)
      {
          CheckBox chkBx = (CheckBox)rw.FindControl("Check");
          if (chkBx != null && chkBx.Checked)
          {              
              strEmail = ((Label)rw.FindControl("lblEmail")).Text; 
              //lblEmail is ID of the label for which you are binding EmailID's.
              // here strEmail is ready with your email id. Do the operation here.
          }
      }

Let me know the results.
--Amit
   
v3
Comments
sandeep nagabhairava 14-Jul-12 3:50am
   
thank you Amit... my 5!
Hi,

Quote:
if (chkBx != null && chkBx.Checked)
{


do not check for null and checked at the same time. if the object is null, you will get Null Reference exception. SO, try to check one condition at a time.

Quote:

lblEmail = grid1.Rows[0].Cells[3].Text.ToString();


every time you are assigning text to same object. if "lblEmail" is label then how can you assign text to label object.

use

lblEmail.Text = grid1.Rows[0].Cells[3].Text.ToString();


so, on every iteration you are assigning to same label/object. how can you differentiate with others. so append the text like below.

lblEmail.Text += " ; " + grid1.Rows[0].Cells[3].Text.ToString();


hope it helps.
   
v2
Comments
Shemeer NS 14-Jul-12 4:48am
   
FYI....

AndAlso(&&) in C#, if the first condition doesn't satisfy then it won't go to the second condition... I hope you are clear on this now...

if (chkBx != null & chkBx.Checked) // if used bitwise & then you are right in case of null it will throw error

correct me if I'm wrong...
in Desktop Application You Can Use it for the checking
whether your row is selected or not Using C#.

if (dataGridView.SelectedRows.Count>-1)
{
//Write your Code here.
}
else
{
//Write your Code here.
}
   
In WPF you can do the following:

Create an event to handle the "Check" event of the checkbox as below:

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
var gridRowNumber = Grid.GetRow((CheckBox)sender);
}
   
Comments
CHill60 10-Jun-14 11:43am
   
2yr old question was not tagged as WPF

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