Click here to Skip to main content
15,843,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this piece of code below which checks whether a checkbox for a certain row is clicked or not and depending on whether it is clicked or not shows either a true or false value.

foreach (DataGridViewRow r in testGV.Rows)
{
  DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)r.Cells["isSelected2"];
   
  if (Convert.ToBoolean(cell.Value)) // Checks if the value of the 
                                     // selected cell is true.
  // Currently returns null except when I press the checkbox and stop the test then
  // the checkbox turns to true because it is clicked.
   .....
   .....
  }
}


The problem I am having is that sometimes the if statement returns a null value for no apparent reason. It only happens when I start the program and press the start test button which ultimately moves the row from one gridview to another. Then it will have a timer which collects data and is represented in the row but the moment I press stop test the if statement shows a true which is good but the moment I let the timer run for the test the value of the if statement turns to null which is bad. Here is where I initialize the columns for each gridview and also how I add a row to the gridviews.

public DataGridViewCheckBoxColumn chk1 = new DataGridViewCheckBoxColumn();
public DataGridViewCheckBoxColumn chk2 = new DataGridViewCheckBoxColumn();

Created as class variables.

This is where I load the form.
private void Form2_Load(object sender, EventArgs e)
{
    connectedGV.ColumnCount = 3;
    connectedGV.Columns[0].Name = "serial";
    connectedGV.Columns[0].ValueType = typeof(string);
    connectedGV.Columns[0].HeaderText = "Serial Number";
    connectedGV.Columns[1].Name = "mainboardfw";
    connectedGV.Columns[1].ValueType = typeof(string);
    connectedGV.Columns[1].HeaderText = "Firmware";
    connectedGV.Columns[2].Name = "opticalfirmware";
    connectedGV.Columns[2].ValueType = typeof(string);
    connectedGV.Columns[2].HeaderText = "Optical Firmware";
    chk1.HeaderText = "Selected";
    chk1.Name = "isSelected1";
    connectedGV.Columns.Add(chk1);
    connectedGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;


    testGV.ColumnCount = 5;
    testGV.Columns[0].Name = "serial";
    testGV.Columns[0].ValueType = typeof(string);
    testGV.Columns[0].HeaderText = "Serial Number";
    testGV.Columns[1].Name = "mainboardfw";
    testGV.Columns[1].ValueType = typeof(string);
    testGV.Columns[1].HeaderText = "Firmware";
    testGV.Columns[2].Name = "opticalfirmware";
    testGV.Columns[2].ValueType = typeof(string);
    testGV.Columns[2].HeaderText = "Optical Firmware";
    testGV.Columns[3].Name = "datacount";
    testGV.Columns[3].ValueType = typeof(string);
    testGV.Columns[3].HeaderText = "Data Count";
    testGV.Columns[4].Name = "startTime";
    testGV.Columns[4].ValueType = typeof(string);
    testGV.Columns[4].HeaderText = "StartTime";
    chk2.HeaderText = "Selected";
    chk2.Name = "isSelected2";
    testGV.Columns.Add(chk2);
    testGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}


Rows added here to connectedGV.
public void UnitConnected(object sender, APData ap)
{
    string[] row0 = { ap.DeviceSerial, ap.DeviceFirmware, ap.OpticalFirmware};
    connectedGV.Invoke((MethodInvoker)delegate
    {
        connectedGV.Rows.Add(row0);
        countBox1.Text = connectedGV.Rows.Count.ToString();
    });

    OutputBox.Invoke((MethodInvoker)delegate
    {
       OutputBox.AppendText("Unit " + ap.DeviceSerial + " connected" + 
                                                 Environment.NewLine);
    });
}


Finally row added to the second gridview where we are having issues.
public void OnBurnInStarted(APData ap)
{
    connectedGV.Invoke((MethodInvoker)delegate
    {
        if (removeItemFromConnectedGrid(ap.DeviceSerial, connectedGV))
        {
            string[] row0 = { ap.DeviceSerial, ap.DeviceFirmware, ap.OpticalFirmware, "0", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), };
            UnitsUnderTestGridView.Rows.Add(row0);
            countBox1.Text = connectedGV.Rows.Count.ToString();
            countBox2.Text = UnitsUnderTestGridView.Rows.Count.ToString();
        }

    });
    //APNG_Application.Program.cm.AddUnit(unit);

    OutputBox.Invoke((MethodInvoker)delegate
    {
        OutputBox.AppendText("Starting Burn-in on " + ap.DeviceSerial +                 
                                                  Environment.NewLine);
    });
}


I ultimately wanted to have a bool value correspond to the checkbox which would be ap.isSelected but I do not know how to get it to bind to a checkbox. I suspect its the way I am adding rows which is causing a null to appear but when I stop the test it clearly works.

If someone could help me figure out why the checkbox appears to be null when the timer runs out that would be great!

What I have tried:

I have tried moving things around, adding a variable to the checkbox and even adding a whole nother column but nothing seems to work. I have also tried sneaking into the row declarations, ap.isSelected but that doesn't seem to do anything.
Posted
Updated 4-Aug-22 9:39am

1 solution

You're adding column values except for your checkbox column:
string[] row0 = { ap.DeviceSerial, ap.DeviceFirmware, ap.OpticalFirmware, ???};

string[] row0 = { ap.DeviceSerial, ap.DeviceFirmware, ap.OpticalFirmware, "0", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ??? };
 
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