如何在C#中使用多个文本框筛选DataGridView数据?
Hey there! Let's figure out why your BindingSource filter code isn't working as intended. I've tackled similar grid filtering problems plenty of times, so let's break down the common pitfalls and fix this step by step.
Common Issues & Fixes
1. Your Data Source Might Not Support Filtering
The BindingSource.Filter property only works with specific data types:
DataTable/DataView(most reliable for filtering)BindingList<T>whereThas public properties- Some other IBindingList implementations
If your dgvAdvisor.DataSource is a plain List<T> or a custom collection that doesn't support filtering, the filter will silently fail. To fix this:
- Convert your
List<T>to aDataTable(you can write a helper method for this) - Or wrap your collection in a
BindingList<T>when assigning it to the BindingSource.
2. Mismatched Column/Property Names
Double-check that First_Name and Username exactly match the column names (if using a DataTable) or property names (if using a custom class) in your data source. Filter is case-sensitive—so if your column is named FirstName instead of First_Name, the filter won't find any matches.
To verify:
- For a DataTable: Check
((DataTable)bs.DataSource).Columnsto confirm column names. - For a custom class: Ensure the properties are public and spelled exactly as in your filter string.
3. Unhandled Empty Inputs & Wildcard Escaping
If lblFirstName.Text or lblUsername.Text is empty, your filter becomes LIKE '%%' (which matches everything), but this can lead to unexpected behavior. Worse, if a user inputs % or _ (wildcard characters in LIKE clauses), it will break your filter logic.
Fix this by:
- Skipping empty conditions
- Escaping wildcard characters so they're treated as literal text
4. Incorrect Data Binding Setup
Your original code cuts off at dgvAdvisor.DataSourc...—make sure you're properly assigning the BindingSource to the DataGridView. Ideally, you should set up the BindingSource as the grid's data source once (during initialization), then just update the filter later.
Revised Working Code
Here's a polished version of your code that addresses all these issues:
// Initialize BindingSource once (better to do this in form load or initialization) private BindingSource bs = new BindingSource(); private void btnSubmit_Click(object sender, EventArgs e) { // Ensure BindingSource is linked to the grid if (dgvAdvisor.DataSource != bs) { bs.DataSource = dgvAdvisor.DataSource; dgvAdvisor.DataSource = bs; } // Build filter condition dynamically List<string> filterParts = new List<string>(); // Handle First Name filter if (!string.IsNullOrWhiteSpace(lblFirstName.Text)) { // Escape wildcard characters string safeFirstName = lblFirstName.Text.Replace("%", "[%]").Replace("_", "[_]"); filterParts.Add($"First_Name LIKE '%{safeFirstName}%'"); } // Handle Username filter if (!string.IsNullOrWhiteSpace(lblUsername.Text)) { string safeUsername = lblUsername.Text.Replace("%", "[%]").Replace("_", "[_]"); filterParts.Add($"Username LIKE '%{safeUsername}%'"); } // Combine conditions or clear filter if no inputs bs.Filter = filterParts.Count > 0 ? string.Join(" AND ", filterParts) : ""; }
Additional Debugging Tips
- Set a breakpoint and inspect the
bs.Filtervalue after it's built—make sure it matches what you expect. - Verify that your data source actually contains records that match the filter criteria (it's easy to assume the code is broken when there are just no matching rows).
- If using a custom class, confirm that all properties referenced in the filter are public (private/internal properties won't be accessible to the BindingSource filter).
内容的提问来源于stack exchange,提问作者tryingtotryhard

