Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i tried but it is not working, my code as follows;

i have one radio button, when i click the radio button, in the datagridview checkbox1 and checkbox2 checked true.

My Code as follows;


in the from load i want one condition, when i run radiobutton is to unchecked in the form load,
when user click the radiobutton bothe the checkbox1 and checkbox2 gets true in the datagridview.

code as follows, i tried but it is not working.


C#
private void cb_am_CheckedChanged(object sender, EventArgs e)
{

        if(cb_am.Checked = true)
    {
        dataGridView1.Rows[0].Cells[0].Value = true;
        dataGridView1.Rows[0].Cells[1].Value = true;
    }

}


what is the mistake in my above code?

Please help me.

Regards,
Narasiman.

Note it is windows application.
Posted
Updated 10-Mar-13 2:37am
v2
Comments
Maciej Los 10-Mar-13 8:40am    
This is probably repost: http://www.codeproject.com/Questions/559214/UsingplusRadioplusbuttonpluscheckedplusallpluschec[^].
Next time, please, improve your question (use "Improve question" widget), rather then post new message.

Your mistake is that you didn't take into account the 2nd possibility which is unchecking the checkboxes.
You code is:
C#
if(cb_am.Checked = true)
          {
              dataGridView1.Rows[0].Cells[0].Value = true;
              dataGridView1.Rows[0].Cells[1].Value = true;
          }

and you need to add the following lines:
C#
else
{
    dataGridView1.Rows[0].Cells[0].Value = false;
    dataGridView1.Rows[0].Cells[1].Value = false;
}
 
Share this answer
 
v2
Your first mistake is that you are using

C#
if(cb_am.Checked = true)


which should be

C#
if(cb_am.Checked == true)


Your 2nd mistake is that you didn't take into account the 2nd possibility which is un-checking the checkboxes.
You code is:
C#
if(cb_am.Checked == true)
          {
              dataGridView1.Rows[0].Cells[0].Value = true;
              dataGridView1.Rows[0].Cells[1].Value = true;
          }

and you need to add the following lines:
C#
else
{
    dataGridView1.Rows[0].Cells[0].Value = false;
    dataGridView1.Rows[0].Cells[1].Value = false;
}
 
Share this answer
 
Comments
[no name] 10-Mar-13 9:48am    
i tried your code but when i uncheck the radio button, in datagridview the checkbox1 and checkbox2 is not unchecked why?

Code as follows;

private void cb_am_CheckedChanged(object sender, EventArgs e)
{
if (cb_am.Checked == true)

{
dataGridView1.Rows[0].Cells[0].Value = true;
dataGridView1.Rows[0].Cells[1].Value = true;
}

else
{
dataGridView1.Rows[0].Cells[0].Value = false;
dataGridView1.Rows[0].Cells[1].Value = false;
}

}

in my above code, else condition code is not working, when i uncheck the radio button the both the checkbox value 1 and 2 is not unchecked.

when i uncheck the radio button, in datagridview the checkbox1 and checkbox2 is checked only . not going to uncheck the two checkboxes.

what is the mistake in my else condition code.

In form load code as follows;

this.cb_am.TabStop = false;
this.cb_pm.TabStop = false;
Michael Haephrati 10-Mar-13 9:54am    
Radio buttons, unlike Check Boxes, are mutual exclusive, meaning that in order to uncheck one Radio button, you need to check the other and vice versa. Further, you need to update the state of all controls. If you publish the entire project, it will be easier to help you
hi,
First of all in code, you do not need to exclusively check for the state of the control, when you are assigning just true or false, it could be as simple as:
C#
private void cb_am_CheckedChanged(object sender, EventArgs e)
{
    dataGridView1.Rows[0].Cells[0].Value = cb_am.Checked;
    dataGridView1.Rows[0].Cells[1].Value = cb_am.Checked;
}


you are basically assigning the state of the radio button to the checkbox in the cell.

Second, radio button are not like check boxes, to change the state of one radio button, an another radio button must be "Checked = true;", so you need at least two radio buttons to check and uncheck your check boxes in those cells.

Third, you are only setting the state of the check boxes in the CheckChanged event of the cb_am control, some times this control may not change its state, such as if you have three radio buttons, cb_am, cb_am1 and cb_am2, if the state of the radio button changes from cb_am1 to cb_am2, the cb_am will not trigger the CheckChanged event, so the checkboxes in those cells will not be updated.

I hope this helps.

Regards
Jegan
 
Share this answer
 
v2
Use This code
C#
if(cb_am.Checked = true)
{
   (dataGridView1.Rows[0].Cells[0] as DataGridViewCheckBoxCell).Value = true;
   (dataGridView1.Rows[0].Cells[0] as DataGridViewCheckBoxCell).Value = true;
}


Thanks & Regard
Sham :)
 
Share this answer
 

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