1

I have a datagridview with a checkbox column. I have written some code so that when a row is checked a process happens. However, the code runs fine but doesn't read the specific row which has been checked and instead works its way down each row.

How can I check that a specific row has been checked?

This is my code:

void ViewSignature(DataGridView AllContacts)
{
    foreach (DataGridViewRow row1 in AllContacts.Rows)
    {
        try
        {
            if (Convert.ToBoolean(row1.Cells[18].Value = true))
            {
                Process.Start(Properties.Settings.Default.ReferencePoint + @"/Contacts/Contact_" + row1.Cells[0].Value.ToString() + @"/Signature.jpg");
            }
            return;
        }
        catch
        {
            //Error message if the system cannot remove the contact from the database.
            MessageBox.Show("Sorry, something went wrong. Please try again later.\n\nERROR CODE: BINSPIRE1009", "BLUEBERRY INSPIRE 2022", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

This is the datagridview

Please be aware that this is just dummy content and the phone numbers and email addresses are fake.

enter image description here

Once the button above is clicked the process should run.

  • You should do this in RowBound event not in databound. – SelvaS Mar 14 '21 at 19:55
  • Sorry, I am fairly new to this, can you elaborate, please? – Joseph Michael Mar 14 '21 at 20:00
  • What is your exact requirement? Call the process if the checkbox is checked? – SelvaS Mar 14 '21 at 20:04
  • By what means does the checkbox become selected? Should this action run as soon as the box is checked? – Caius Jard Mar 14 '21 at 20:05
  • Yes, the idea for the user to choose a specific row for example (Sadio Mane) check the checkbox and then click a button in a toolstrip which then runs the process. At the moment the process runs perfectly fine. However, the process is run for each row in the datagridview rather than the row which has been checked by the user. Hope this makes sense:) – Joseph Michael Mar 14 '21 at 20:08
  • I've included the button which I want the user to click above. – Joseph Michael Mar 14 '21 at 20:11
  • The `if (Convert.ToBoolean(row1.Cells[18].Value = true))` condition has its parentheses misplaced. Change to, e.g., `if ((bool)row1.Cells[18].Value)`. – Jimi Mar 14 '21 at 20:16
  • Tried this, but returns a system.NullReferenceException. – Joseph Michael Mar 14 '21 at 20:21
  • Then you know that you've never set to anything the content of those Cells. Did you add the Column at run-time, so it's - possibly - not bound to a data column that provides a value? – Jimi Mar 14 '21 at 20:34
  • Also, instead of this: `row1.Cells[18]`, use this form: `row1.Cells["ColumnName"]` – Jimi Mar 14 '21 at 20:42
  • Thanks for your reply. Still not working. Also, I used row.Cells[18] over row.Cells["ColumnName"] since this is a checkbox column which had programmatically added in the form load handler. I've found that using if (Convert.ToBoolean(row1.Cells[18].Value)) works fine with a normal button but won't execute using my toolstripmenuitem. Any suggestions please? – Joseph Michael Mar 14 '21 at 20:46
  • 1
    1. You can assign a name to a DataGridViewColumn generated at run-time. 2. If a method doesn't throw an exception, it doesn't mean that *it's working* or *works better*, it means that it doesn't throw. If this: `(bool)row1.Cells[18].Value` throws instead, than your cells don't have a value (it's a `null` object). You probably haven't specified a `ValueType` and a `Value` for the Column. – Jimi Mar 14 '21 at 21:02
  • 1
    You need to commit the grid's changes before running that signature routine to get the current check state. See the answers [here](https://stackoverflow.com/q/11843488/14171304) for example. Read also in the doc: `CommitEdit`, `EndEdit` methods. `IsCurrentCellDirty` and `IsCurrentRowDirty` properties. – dr.null Mar 15 '21 at 01:27
  • Thanks for your replies. – Joseph Michael Mar 15 '21 at 13:44

1 Answers1

0

I managed to solve this issue by using the statement datagridview.endedit().

I had read on another forum that when using a button, the edit state of the datagridview is terminated since the focus switches to the button. However, this is not the case when using the items in toolstrips and menustrips so the statement above is required to terminate the edit process of the datagridview so that the toolstrip can successfully carry out its processes.