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?