In the application, I have two ways to search, one using the btnSearch button and the other using inserting values into textBox1. Here are both types of searches:
1. I need it to show it to me in the datagride of data after searching with the btnSearch button (this works for me).
2. After inserting a value into texBox1, it looks in those records that were searched by the btnSearch branch. (now looking for me in the whole database) I also need to avoid this in the datagride.
I thought to put the first filtered data in a temporary table and search that table using textBox1_TextChanged. I tried it, so far I can't write it correctly so that it works for me.
C#:
DataTable dt = new DataTable();
private void btnHladaj_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Databaza", con);
DataTable dt = new DataTable();
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
dt.Load(dr);
con.Close();
dataGridView1.DataSource = dt;
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
string filter = "";
// Check if text fields are not null before adding to filter.
if (!string.IsNullOrEmpty(comboDruh.Text))
{
filter += dataGridView1.Columns["Druh"].HeaderText.ToString() + " LIKE '%" + comboDruh.Text + "%' ";
}
if (!string.IsNullOrEmpty(comboKategoria.Text))
{
if (filter.Length > 0) filter += "AND ";
filter += dataGridView1.Columns["Kategoria"].HeaderText.ToString() + " LIKE '%" + comboKategoria.Text + "%' ";
}
if (!string.IsNullOrEmpty(comboMeno.Text))
{
if (filter.Length > 0) filter += "AND ";
filter += dataGridView1.Columns["Meno"].HeaderText.ToString() + " LIKE '%" + comboMeno.Text.Substring(0, comboMeno.Text.Length - 5) + "%' ";
}
bs.Filter = filter;
dataGridView1.DataSource = bs;
private void textBox1_TextChanged(object sender, EventArgs e)
{
DataView dv = dt.DefaultView;
dv.RowFilter = string.Format("Poznamka LIKE '%{0}%'", textBox1.Text);
dataGridView1.DataSource = dv.ToTable();
}
1. I need it to show it to me in the datagride of data after searching with the btnSearch button (this works for me).
2. After inserting a value into texBox1, it looks in those records that were searched by the btnSearch branch. (now looking for me in the whole database) I also need to avoid this in the datagride.
I thought to put the first filtered data in a temporary table and search that table using textBox1_TextChanged. I tried it, so far I can't write it correctly so that it works for me.
Last edited by a moderator: