Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Gridview
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 13-Jul-12 22:07pm
Comments
Shemeer NS at 14-Jul-12 4:50am
   
It's not working means!!! getting any error? or not working as expected?
shashank raj gupta at 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 at 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 at 31-Dec-12 1:56am
   
plz help me out ....... fast
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v3
Comments
sandeep nagabhairava at 14-Jul-12 3:50am
   
thank you Amit... my 5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Comments
Shemeer NS at 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...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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);
}
  Permalink  
Comments
CHill60 at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 18 Nov 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100