简体   繁体   中英

Filter data from MySQL database and show it in datagridview

I need to make filter in my app when user loads database from the button "Učitaj bazu članova" and selects certain arguments like typing in a name David (if there's couple of members with the same name it should only filter them) and selecting to which Sekcija he belongs (and there are couple of members with the same name and in same Sekcija like Tamburaši A grupa) and their status by checking radiobutton Aktivan (active status of the members) and on pressing Filtriraj članove it should filter only active members with name David and who are in the same combobox Sekcija (Tamburaši A grupa). Or if user decides he only want's to filter members by gender (radiobuttons Muško and Žensko) it should only filter for example male (Muško) members. Or if user selects only active (radiobutton Aktivan) male (radiobutton Muško) members it should only filter male active members in datagridview. If this makes any sense. I apologize if you can't understand, I'm trying my best to explain what I want to do.

Design of my app and filter what I'm trying to make

I've tried couple of solutions but none worked so I discarded them and won't be mentioning them cause I can't even remember how many solutions I've tried, and so far all I managed to do when wanting to filter members is this

(baza_filter_clanova.DataSource as DataTable).DefaultView.RowFilter = string.Format("ime LIKE '{0}%' OR prezime LIKE '{0}%'", pretraga_ime_prezime.Text);

what basically did filter only by name or a last name of a certain member when typing in textbox. But since in my database I have two columns (name and last name) I decided to change design of my app and allow users to type in name in textbox which is made only for name and last name in textbox made only for last name (eventually couldn't get it working so I'm still searching for a solution on that also cause it'll only filter by last name, weird..).

This is the code I used when I wanted allow users to type in name in one textbox and last name in other textbox but it would only work when typing in last name, if I typed in name then it wouldn't filter anything and it was showing all records from database in datagridview.

private void Filtriraj_clanove_Click(object sender, EventArgs e)
    {
      (baza_filter_clanova.DataSource as DataTable).DefaultView.RowFilter = string.Format("ime LIKE '{0}%'", pretraga_ime.Text);
      (baza_filter_clanova.DataSource as DataTable).DefaultView.RowFilter = string.Format("prezime LIKE '{0}%'", pretraga_prezime.Text);
    }

The filter property can hold one value at a time. It works when you enter last name because only the last name filter is effective. Change your method to following:

private void Filtriraj_clanove_Click(object sender, EventArgs e)
    {
      (baza_filter_clanova.DataSource as DataTable).DefaultView.RowFilter = string.Format("ime LIKE '{0}%' AND prezime LIKE '{1}%'", pretraga_ime.Text, pretraga_prezime.Text);
    }

You may want to replace AND with OR if you want matches to be returned for either first name or last name. Make sure you scrub entered values so they don't have any special characters that may break your SQL query.

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