Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:

C#
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:

C#
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?
Posted
Updated 19-Mar-22 7:33am
v7
Comments
Maciej Los 14-Mar-22 17:24pm    
The error message is quite obvious... You've got desease id, now find that id in the list, then...
ernteSKY 15-Mar-22 14:28pm    
How you mean that?
Maciej Los 15-Mar-22 15:31pm    
Well, Form_Diseases_dataGridView.SelectedRows[0].Cells[0].Value does not return Disease, but some value. Based on that value, you can find Disease.

Please, tell us how you bind data to a DataGridView.
ernteSKY 19-Mar-22 13:36pm    
Form_Diseases_dataGridView.DataSource = null;
SqlDataAdapter ownReader = new SqlDataAdapter("SELECT * FROM [Diseases]", ownConnection);
DataTable ownDataTable = new DataTable();
ownReader.Fill(ownDataTable);
Form_Diseases_dataGridView.DataSource = ownDataTable;
[no name] 15-Mar-22 14:45pm    
Put a break point on "disease =" and confirm that what's in ..cells[0].Value is a Disease type.

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