简体   繁体   中英

How to search data in mysql database and add the search data in DataGridView using C#?

I am working on it for an hours now maybe 6 hours i can't get it working please tell me what is missing?

So for Example I have here client Warren Buffet (he is just an example :D) if i search his name on the txtbox and sqldatabase find his name on the data row then Wafrren Bufet Data will be added to the DataGridView.

Here's the code!

        private void button2_Click(object sender, EventArgs e)
        {
            MySqlConnection connection = null;
            string hostname = "localhost";
            string database = "aparece_hoteldb";
            string username = "root";
            string password = "";
            connection = new MySqlConnection("host=" + hostname +
                                            ";database=" + database +
                                            ";username=" + username +
                                            ";password=" + password + ";");

            string query = "select * from reservations " +
                           "where Client like '%" + txtSearch.Text + "%' OR " +
                               "ClientNumber like '%" + txtSearch.Text + "%' OR " +
                               "RoomNumber like '%" + txtSearch.Text + "%' OR " +
                               "ClientRoomType like '%" + txtSearch.Text + "%' OR " +
                               "AddonService like '%" + txtSearch.Text + "%' OR "+ 
                               "TotalHotelRate '%" + txtSearch.Text + "%'";

            connection.Open();
            List<string>[] detailList = a.mysqlSelect(query);
            for (int i = 0; i < detailList.Length; i++)
            {
                dgvUser.Rows.Add(detailList[i][0], detailList[i][1], detailList[i][2], detailList[i][3], detailList[i][4]);
            }

        }

    }

}

Here's the image!

![enter image description here][1]

So you mean there are already data in the DataGridView and if you search a using TextBox, it will be added on the list on the DataGridView? Here's my code:

var detailList = (a.mysqlSelect(query)).ToList();
foreach(var dList in detailList)
{
   dgvUser.Rows.Add(dList);
}

But if you want to to only have the data search on the DataGridView, here's my code:

var detailList = (a.mysqlSelect(query)).ToList();
dgvUser.DataSource = detailList;

Hope it helps!

EDIT: And by the way, your code in query lacks a "like" in the TotalHotelRate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM