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); }
public List<Livre> FilterDateDePublication(string date) { using (var connection = factory.CreateConnection()) { var livres = new List<Livre>(); connection.ConnectionString = connectionString; connection.Open(); 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; }
DataRow[] foundRows = YourDataTable.Select(searchExpression);
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.
// Filter by an expression. var yearOfPublication = 2000; DataGridView1.RowFilter = $"date_publication.Year = {yearOfPublication}";
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)