DatagridView column column is always zero

I have a strange problem. Basically I have a datagridview and a button. When I click this button, it checks all the rows for the value of column 1 - the checkbox column. Then it sets the value to true / false depending on what is currently located.

All perfectly.

But then I have one more button to do something with these lines that are checked. I click on it and it only ever identifies the first line as a checkmark. The rest, apparently, are now zero.?

So, how can I programmatically set the value of the checkbox column in the datagrid view and then read it again, make Im seem to exit the label based on my results.

This sets the checkboxes, and I can see them, turn them off manually, etc.

foreach (DataGridViewRow row in dgv.Rows)
        {
            var ch1 = new DataGridViewCheckBoxCell();
            ch1 = (DataGridViewCheckBoxCell)row.Cells[0];

            if (ch1.Value == null)
                ch1.Value = false;
            switch (ch1.Value.ToString())
            {
                case "True":
                    ch1.Value = false;
                    break;
                case "False":
                    ch1.Value = true;
                    break;
            }
        }

Then the next button to check the values ​​is just a search for zeros

foreach (DataGridViewRow row in rows)
            {
                var ch1 = new DataGridViewCheckBoxCell();
                ch1 = (DataGridViewCheckBoxCell)row.Cells[0];

                if (ch1.Value == null)
                    ch1.Value = false;
                switch (ch1.Value.ToString())
                {
                    case "True":
                        ch1.Value = true;
                        break;
                    case "False":
                        ch1.Value = false;
                        break;
                }
                var val = row.Cells["EbayListingID"].Value.ToString();
                if (ch1.Value.ToString() == "true") continue;
                var listing = dsEntities.EbayListings.First(x => x.EbayListingID.ToString() == val);
                SubmitListingForReview(listing, false);
            }
+4
source share
1 answer

Firstly,

if (ch1.Value.ToString() == "true") continue;

Why is the string constant "true" but not "true"?

Secondly, in the next button, click the handler, what are these "strings"?

foreach (DataGridViewRow row in rows)

I try this code and it works fine:

private void button1_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                var ch1 = new DataGridViewCheckBoxCell();
                ch1 = (DataGridViewCheckBoxCell)row.Cells[0];

                if (ch1.Value == null)
                    ch1.Value = false;
                switch (ch1.Value.ToString())
                {
                    case "True":
                        ch1.Value = false;
                        break;
                    case "False":
                        ch1.Value = true;
                        break;
                }
            }
        }

private void button2_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                var ch1 = new DataGridViewCheckBoxCell();
                ch1 = (DataGridViewCheckBoxCell)row.Cells[0];

                if (ch1.Value == null)
                    ch1.Value = false;
                switch (ch1.Value.ToString())
                {
                    case "True":
                        ch1.Value = true;
                        break;
                    case "False":
                        ch1.Value = false;
                        break;
                }
                var val = row.Cells[1].Value;
                if (ch1.Value.ToString() == "True") continue;
                MessageBox.Show("1");
            }
        }
0
source

All Articles