Click here to Skip to main content
15,439,117 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:[^]
I'm trying to filter a DataGridView with RadioButton when DateDePublication is Checked and the value of the search text equals for example 2000 table should return all books who have DateDePublication equals to 2000

this is Search Button code :

<pre lang="C#"> private void RechBtn_Click(object sender, EventArgs e)
            dataGridView1.DataSource = repository.GetAllLivres(rechtext.Text);


and search method code to return all books :

public List<Livre> FilterDateDePublication(string date)
        using (var connection = factory.CreateConnection())
            var livres = new List<Livre>();
            connection.ConnectionString = connectionString;
            var command = factory.CreateCommand();
            command.Connection = connection;
            command.CommandText = "select * from livre where date_publication like '%" + date + "%'";
            using (DbDataReader reader = command.ExecuteReader())
                while (reader.Read())
                    Livre l = new Livre();
                    l.Isbn = reader["isbn"].ToString();
                    l.Titre = reader["titre"].ToString();
                    l.DatePublication = DateTime.Parse(reader["date_publication"].ToString());
                    l.NombrePage = Int32.Parse(reader["nombre_page"].ToString());
                    l.Couverture = reader["couverture"].ToString();
                    l.Prix = Double.Parse(reader["nombre_page"].ToString());
                    l.QuantiteDisponible = Int32.Parse(reader["quantite_disponible"].ToString());
            return livres;


What I have tried:

i wanted to use
DataRow[] foundRows = YourDataTable.Select(searchExpression);

but i don't where it should be
Updated 3-Mar-22 0:55am


one way would be to use a DataTable and a BindingSource.
The DataTable is to hold all the books.
The BindingSource is a go-between that passes the DataTable to the DataGridView; it has a Filter capability.

DataTable dt = new DataTable();
BindingSource bs = new BindingSource();
// here code to load all books in dt
bs.DataSource = dt;
dataGridView1.DataSource = bs;
// now the DGV shows all the books
bs.Filter = searchExpression;
// where searchExpression could be something like
// string.Format("Titre LIKE '%{0}%'", textBox1.Text);
// now DGV shows matching books only.

More filter examples can be found here: BindingSource Filter with Starts, contains, ends with and case sensitive options - TechNet Articles - United States (English) - TechNet Wiki[^]
Share this answer
Maciej Los 3-Mar-22 6:55am    
Luc Pattyn 3-Mar-22 7:14am    
Abderrahmane Radiohead 5-Mar-22 17:19pm    
Mind me I'm still confused because I'm still learning C#, where should I place your code into mine because i have two files class called GlobalRepisotory which contains all functions button(delete, add, search, edit) and second for Form, I don't know how to implement my idea (each radio button have a column name when radio button is checked the search should look up by column).
Luc Pattyn 5-Mar-22 18:01pm     CRLF
The first part is preparation, could go in a Form_Load handler or anywhere you see fit. That is the lines: DataTable dt = new DataTable(); BindingSource bs = new BindingSource(); // here code to load all books in dt ... bs.DataSource = dt; dataGridView1.DataSource = bs; // now the DGV shows all the books If you start a filtering by clicking a button, then the remainder belongs in the Button_click handler; that is where you construct your filter (depending on the radio buttons and tthe textbox) and then assign it to bs.Filter property.
If you want to filter only DataGridViewRows, use DataView.RowFilter Property (System.Data) | Microsoft Docs[^]

// Filter by an expression.
var yearOfPublication = 2000;
DataGridView1.RowFilter = $"date_publication.Year = {yearOfPublication}";

Good luck!
Share this answer

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