Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void btnView_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridLeaveApproved.Rows)
            {
                CheckBox ch = (CheckBox)row.FindControl("CheckBox1");
                if (ch.Checked)
                {
                    //Label lblleaveid = (Label)row.FindControl("lblLid");
                    //int lid = Convert.ToInt32(lblleaveid.Text);
                    //Label lbllid = (Label)row.FindControl("lblLId");
                    //int lid = Convert.ToInt32(lbllid.Text);
                    Label lblEid = (Label)row.FindControl("lblEmpId");
                    string eid = lblEid.Text;
                    cmd = new MySqlCommand("Select * from empleaves where EmpId=@eid ", conn);
                    cmd.Parameters.AddWithValue("@eid", eid);
                    MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "newleave");
                    GridLeavesView.DataSource = ds.Tables["newleave"];
                    GridLeavesView.DataBind();
                }
            }
            GridLeavesView.Visible = true;
        }




i want to check only one checkbox. i am trying to select two checkboxes show error message like(only select one checkbox) when after clicking View Button.


please help me friends..

What I have tried:

protected void btnView_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridLeaveApproved.Rows)
{
CheckBox ch = (CheckBox)row.FindControl("CheckBox1");
if (ch.Checked)
{
//Label lblleaveid = (Label)row.FindControl("lblLid");
//int lid = Convert.ToInt32(lblleaveid.Text);
//Label lbllid = (Label)row.FindControl("lblLId");
//int lid = Convert.ToInt32(lbllid.Text);
Label lblEid = (Label)row.FindControl("lblEmpId");
string eid = lblEid.Text;
cmd = new MySqlCommand("Select * from empleaves where EmpId=@eid ", conn);
cmd.Parameters.AddWithValue("@eid", eid);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "newleave");
GridLeavesView.DataSource = ds.Tables["newleave"];
GridLeavesView.DataBind();
}
}
GridLeavesView.Visible = true;
}
Posted
Updated 24-May-16 3:10am
v2
Comments
F-ES Sitecore 24-May-16 8:53am    
Do you mean you *are* seeing that message? Or that you want to see that message? If you want to only process one checkbox do something like

foreach (GridViewRow row in GridLeaveApproved.Rows)
{
CheckBox ch = (CheckBox)row.FindControl("CheckBox1");
if (ch.Checked)
{
// your code

break; // this will exit the outer for loop
}
}
Member 11652153 24-May-16 9:07am    
ok..but i want to see message.. like ((only select one checkbox)
Karthik_Mahalingam 24-May-16 9:13am    
Always use  Reply  button, to post Comments/query to the user, else the User wont get notified.

CHeck Solution 2 and let me know if it helps.
F-ES Sitecore 24-May-16 9:37am    
Define a "bool processed = false;" in your click event. Inside the "if (ch.Checked)" statement check if processed is true and if it is show a message and do a "break;". If it is false then set it to true and process the checkbox as normal.

Since you have tagged Asp.net and C#, i am giving you the solution using c#.
and i hope you will be having a label to display the error message.

C#
protected void btnView_Click(object sender, EventArgs e)
       {
           lblMessage.Text = "";
           int count = 0;
           foreach (GridViewRow row in GridLeaveApproved.Rows)
           {
               CheckBox ch = (CheckBox)row.FindControl("CheckBox1");
               if (ch.Checked)
               {
                   count++;
                   if (count > 1)
                   {
                       lblMessage.Text = "Please select only one row";
                       return;
                   }
               }
           }

           foreach (GridViewRow row in GridLeaveApproved.Rows)
           {
               CheckBox ch = (CheckBox)row.FindControl("CheckBox1");
               if (ch.Checked)
               {
                   //Label lblleaveid = (Label)row.FindControl("lblLid");
                   //int lid = Convert.ToInt32(lblleaveid.Text);
                   //Label lbllid = (Label)row.FindControl("lblLId");
                   //int lid = Convert.ToInt32(lbllid.Text);
                   Label lblEid = (Label)row.FindControl("lblEmpId");
                   string eid = lblEid.Text;
                   cmd = new MySqlCommand("Select * from empleaves where EmpId=@eid ", conn);
                   cmd.Parameters.AddWithValue("@eid", eid);
                   MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                   DataSet ds = new DataSet();
                   da.Fill(ds, "newleave");
                   GridLeavesView.DataSource = ds.Tables["newleave"];
                   GridLeavesView.DataBind();
               }
           }
           GridLeavesView.Visible = true;


       }
 
Share this answer
 
Comments
Member 11652153 24-May-16 9:15am    
Here i want use Java script message also
Karthik_Mahalingam 24-May-16 9:16am    
post your mark up for gridview
Member 11652153 24-May-16 9:20am    
Thank you Bro.. its working
Karthik_Mahalingam 24-May-16 9:21am    
if it is working, please close this post formally by marking it as answer.
Member 11652153 24-May-16 9:29am    
i cant understand what you say..
You can also perform like this..

C#
foreach (DataGridViewRow row in GridLeaveApproved.Rows)
        {
            DataGridViewCheckBoxCell ch = (DataGridViewCheckBoxCell)row.Cells[1];
            if (ch.Selected == true)
            {
                //do code if it checked
            }
            else
            {
                //do code if it not checked
            }
        }
 
Share this answer
 
Comments
Member 11652153 24-May-16 9:12am    
what is DataGridViewCheckBoxCell ..
here i want one message after clicking two checkboxes in gridview when click the view button
message like (only select one checkbox)

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