5

I have a strange issue. Basically I have a datagridview and a button. When I click this button it checks all rows for column 1s value - the checkbox column. It then sets it to true / false depending on what it currently is.

Thats all fine.

But then, I have another button to do something with these rows that are ticked. I click it and it only ever identifies the first row as being ticked. The rest are apparently null now..?

So, how can I programmtically set the value of a checkbox column in a datagrid view and then read it again cause Im apparently way off the mark based on my results.

This sets the tick boxs and I can see them, untick them 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 values is just finding nulls

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);
            }
Aaron Gibson
  • 1,280
  • 1
  • 21
  • 36
  • You are presumably doing something wrong but, if we don't know what you're doing, we can't know what's wrong with it. – jmcilhinney Oct 02 '14 at 00:44
  • You set and read the data the same as a normal combo box. If you want us to tell you what you are doing wrong then we need to see your code – deathismyfriend Oct 02 '14 at 00:50
  • Try posting your code. This will help us know what you've tried and haven't. However, my assumption would be that your model is not properly developed to store the result of this checkbox. Are you databinding your datagridview or creating rows manually? – Brett Wolfington Oct 02 '14 at 01:10

1 Answers1

0

First,

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

Why is string constant is "true", but not "True"?

Second, in the next button click handler, what is it "rows"?

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");
            }
        }
Alexander
  • 121
  • 1