Reputation: 137
i am trying to filter out the data that i want, and hide all the other data instead. Here is my list of code,
private void searchButton_Click_1(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = dataGridView1.Columns[1].HeaderText.ToString() + " LIKE '%" + searchTextBox.Text + "%'";
dataGridView1.DataSource = bs;
}
but when i run it, it prompts me error message
"Missing operand after 'ID' operator."
and i have tried this too, same error message.
private void searchButton_Click_1(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
// bs.Filter = dataGridView1.Columns[1].HeaderText.ToString() + " LIKE '%" + searchTextBox.Text + "%'";
bs.Filter = "Sample ID like '*" + searchTextBox.Text + "*'";
dataGridView1.DataSource = bs;
}
can anyone please help me clarify the problem? thanks.
Upvotes: 4
Views: 42354
Reputation: 1
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[6].Value.ToString().Contains(textBox1.Text))
{
dataGridView1.Rows[i].Visible = true;
}
else if (dataGridView1.Rows[i].Cells[0].Value.ToString().Contains(textBox1.Text))
{
dataGridView1.Rows[i].Visible = true;
}
else if (dataGridView1.Rows[i].Cells[2].Value.ToString().Contains(textBox1.Text))
{
dataGridView1.Rows[i].Visible = true;
}
else
{
dataGridView1.Rows[i].Visible = false;
}
}
Upvotes: 0
Reputation: 1
there is 2 approach in this subject:
1 If you are using data table
bsItems.Filter = "VendorAccount like'*" + txtFilterAccount.Text + "*'";
2 Using ORM like dapper you will have to create new list to stored filtered data
lstFilteredVendors = lstVendors.Where(x => x.VendorAccount.Contains(t.Text)).ToList();
bsItems.DataSource = lstFilteredVendors;
Upvotes: 0
Reputation: 61
I set my datasource as list and used LINQ to filter the datagridview.
//Declare Global
List<ProductList> _productList
//Somewhere in code initialize datagridview
DataGridView1.DataSource = _productList
//TextBox TextChanged Function
private void TxtSearchProduct_TextChanged(object sender, EventArgs e)
{
var result = _productList.Where(x =>
x.ProductName.Contains(TxtSearchProduct.Text)).ToList();
DataGridView1.DataSource = result;
}
I hope it helps
Upvotes: 1
Reputation: 51
Try with following
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = "[HeaderText] Like '%" + searchTextBox.Text + "%'";
dataGridView1.DataSource = bs;
Upvotes: 5
Reputation: 6490
Try with following ,
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = "yourColumnName like '%" + textBox1.Text + "%'";
dataGridView1.DataSource = bs;
or
In Search Text Box Changed event, try the following,
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Field = '{0}'", searchTextBox.Text);
Upvotes: 0