0

I have created a form where can list all diseases in DataGridView.
I can add or delete any kind of diseases, but I still can't edit any diseases which already added into the database.

Here is my code:

if (Form_Diseases_dataGridView.SelectedRows.Count > 0 && MessageBox.Show("Are you sure to edit this disease?", "Edit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
    Disease editDisease = (Disease)Form_Diseases_dataGridView.SelectedRows[0].Cells[0].Value;
    Form_Diseases_Add DiseaseEdit = new Form_Diseases_Add(editDisease);
    if (DiseaseEdit.ShowDialog() == DialogResult.OK)
    {
        try
        {
            ConnectToDB.EditDisease(DiseaseEdit.Disease);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            DialogResult = DialogResult.None;
        }
    }
}

I have the following error message: System.InvalidCastException: The object cannot be converted from "System.Int32" type to "MyProject.Disease" type.

This line generates the error:

Disease editDisease = (Disease)Form_Diseases_dataGridView.SelectedRows[0].Cells[0].Value;

What I'm doing wrong?

With ListBox:

if (Form_Diseases_listBox.SelectedIndex != -1 && MessageBox.Show("Are you sure to edit this disease?", "Edit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
    Disease editDisease = (Disease)Form_Diseases_listBox.SelectedItem;
    Form_Diseases_Add DiseaseEdit = new Form_Diseases_Add(editDisease);
    if (DiseaseEdit.ShowDialog() == DialogResult.OK)
    {
        try
        {
            ConnectToDB.EditDisease(DiseaseEdit.Disease);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            DialogResult = DialogResult.None;
        }
    }
}

Well working with ListBox, but I can't find the way have to do it in DataGridView?

Jimi
  • 29,621
  • 8
  • 43
  • 61
Marcell Nemeth
  • 97
  • 3
  • 4
  • 13
  • 1
    You are getting the first cell of the first row and expecting it to be a "Disease". It can't possibly be without some custom implemented datagrid cell type. You either need to build the object from all the cell values, or better yet, get it from the binding – Crowcoder Mar 20 '22 at 11:47
  • If the DataSource of your DGV is the same as the ListBox, then it's probably `Form_Diseases_dataGridView.SelectedRows[0].DataBoundItem` – Jimi Mar 20 '22 at 12:00
  • If you assigned a list of Disease to the DataGridView.DataSource consider setting the list to a BindingSource which becomes the DataSource for the DataGridView. Now when the user wants to remove a Disease cast Current property of the BindingSource to Disease which provides access to the current row in the DataGridView. And you can even setup a BindingNavigator to handle removal of a row where you can override the default behavior of the remove button to ask if they really want to remove the selected Disease. – Karen Payne Mar 20 '22 at 12:37

0 Answers0