Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When you select a data(row) from datagridview1 the results that has those data from the database will appear in the listview1.
They will filter and sort the selected data.

For example:

-You select a symptoms
-Coughing
-The results(name of the disease) that has a coughing symptom that you selected in the datagridview1 will automatically display in the listview1.
-You select another symptom
-Wheezing
-The results(name of the disease) that has coughing and wheezing that you selected in the datagridview1 will automatically display in the listview1 and those who doesn't have those data will automatically disappear.

I think it's like when you select that word. They will find it in the database table. Like the word cough or wheezing or even the letters only. And it will automatically display it in another datagridview.

The results are also in the database.
They will get those data in the database(MySql)

I'm planning to just get those data in my dictionary table in database. And there are columns like the name of the disease, causes, symptoms, treating and etc.
But the only column that I just want to get the data from are only symptoms and the name of the disease.


I really don't know the syntax. I really don't know where to start. Please help. Thank you so much. I hope you can understand my question. :)

I tried coding it. Heheh. But everything is just so wrong.

What I have tried:

 private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count != 0)
            {
                con.Open();
                cmd = new SqlCommand(@"Select * from DICTIONARY where Symptoms LIKE '% 
{0}%'", con);
                rdr = cmd.ExecuteReader();
                if (rdr.Read())
                {
                    dataGridView1.Rows.Add(rdr[0].ToString(), rdr[1].ToString());
                }

            List<string> selectedRows = new List<string>();

            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                string currentRow = string.Empty;

                foreach (DataGridViewCell cell in row.Cells)
                {
                    currentRow += String.Format("{0} ", cell.FormattedValue);
                }

                selectedRows.Add(currentRow);
            }

            for (int i = 0; i < selectedRows.Count; i++)
            {
                this.listview1.Items.Add(selectedRows[i]);
            }

            }
        }
Posted
Updated 17-May-18 1:58am

1 solution

You are not adding the symptom name into your SQL command. The {0} construct is used by String.Format and requires the actual parameter name. For SQL commands you should use parameterised queries. Also, you are trying to add the database results to the DataGridView instead of the ListView.
 
Share this answer
 
Comments
Member 13714370 18-May-18 0:18am    
Is my syntax right? Cause there are lots of errors. And my code doesn't match for what I was planning to do. I think.
Richard MacCutchan 18-May-18 3:35am    
No, it is not correct:
cmd = new SqlCommand(@"Select * from DICTIONARY where Symptoms LIKE '%
{0}%'", con);

As I said above {0} means nothing in this statement. You need to replace that with a parameter name, and add the actual text into a parameter object which you add to the SQLCommand.

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