0

i have a dowpdownlist, a button and a checkbox inside the datagridview.

i just only manually created a check box column on the datagridview. (here is the code)

DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
            CheckBox chk = new CheckBox();
            CheckboxColumn.Width = 20;
            DataGrid1.Columns.Add(CheckboxColumn);

here is the procedure.
step 1: the user will choose item on the checkbox.
step 2: the user will choose item on the dropdown.
Step 3: the user will click on the button and it will change the itemname
on the checkbox prior to the item selected on the dropdownlist.

here is my problem after clicking on the button, nothings happen.

here is my code.

private void button1_Click(object sender, EventArgs e)
        {
    int x = 0;
                foreach (DataGridViewRow item in this.DataGrid1.SelectedRows)
                {
                    DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)item.Cells[1];
                    if (chk.Selected)
                    {
                    // codes here
                    }
                    else
                    {
                    //code here
                    }
                }
                x = x + 1;
         }
3
  • Is the button1_Click event firing? If it is not, you may not have linked the event handler to the button click event. Commented Nov 9, 2012 at 9:29
  • yes it's firing. it doesn't go into the condition when i use the breakpoint. Commented Nov 12, 2012 at 1:19
  • What is the value of this.DataGrid1.SelectedRows.Count ? also what is the purpose of chk variable. It seems useless. Commented Nov 12, 2012 at 9:31

2 Answers 2

1

* EDITED **

I've tested this and it definitely Works. Copy and paste this into a new project and play with it. It should help you get to where you need to be.

   private void Form1_Load(object sender, EventArgs e)
    {

        DataGridViewCheckBoxColumn checkBox = new DataGridViewCheckBoxColumn(true);
        checkBox.HeaderText = "T/F";
        dataGridView1.Columns.Add(checkBox);

    }

    private void button1_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.SelectedRows)
        {

            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];

            if (Convert.ToBoolean(chk.Value) == true)
            {
                MessageBox.Show("Value Is True");
            }

        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

there is error on CheckState.Equals even if i declare CheckState CheckState = default(CheckState);
0

First thing that I would recommend to call:

DataGrid1.EndEdit();

Since, I have experienced that sometimes input do not appears as expected if this line is missing before retrieving a value of checkbox from the grid column.

So something like this:

private void button1_Click(object sender, EventArgs e)
{
    int x = 0;
    foreach (DataGridViewRow item in this.DataGrid1.SelectedRows)
    {
        DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)item.Cells[1];

        if (chk.Value)
        {
              // codes here for checked condition
        }
        else
        {
              //code here  for UN-checked condition
        }
      }
     x = x + 1;
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.