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 disease = (Disease)Form_Diseases_dataGridView.SelectedRows[0].Cells[0].Value;
Form_Diseases_Add ownDiseaseEdit = new Form_Diseases_Add(disease);
if (ownDiseaseEdit.ShowDialog() == DialogResult.OK)
{
try
{
ConnectToDB.EditDisease(ownDiseaseEdit.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.
What I'm doing wrong?
What I have tried:
if (Form_Diseases_listBox.SelectedIndex != -1 && MessageBox.Show("Are you sure to edit this disease?", "Edit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Disease disease = (Disease)Form_Diseases_listBox.SelectedItem;
Form_Diseases_Add ownDiseaseEdit = new Form_Diseases_Add(disease);
if (ownDiseaseEdit.ShowDialog() == DialogResult.OK)
{
try
{
ConnectToDB.EditDisease(ownDiseaseEdit.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?