开发者

Filtering related data in a DataGridView

开发者 https://www.devze.com 2023-03-26 01:49 出处:网络
I have a DataGridView that pulls data from a couple tables similarly to this setup.It works great.Good post and answer.Continuing with the example in that post, I now want to create a filter that yiel

I have a DataGridView that pulls data from a couple tables similarly to this setup. It works great. Good post and answer. Continuing with the example in that post, I now want to create a filter that yields all transactions in the DataGridView that apply to a specific account by using a LIKE parameter on the account description.

I have a solution by checking the accounts table for the description and obtaining the IDAccount value then using that value in the DataGridView filter, but I was hoping there would be a more automated way using bindings.

Any ideas? Thank you for your suggestions.

Edit: Supposing I have a TextBox control called AccountDescriptionBox, I'd like to be able to do something like

dataGridView1.Filter = string.Format("{0} LIKE '{1}'", "IDAccount", AccountDescriptionBox.Text);

Obviously, this won't work as IDAccount is an integer, not a string. The solution I mention above is

string filter = string.Empty;
Regex searchTerm = new Regex(Regex.Escape(AccountDescriptionBox.Text).Replace('\\', '.'), RegexOptions.IgnoreCase);
var accts = from acct in dataSet1.Account开发者_Python百科s
            let matches = searchTerm.Matches(acct.Description)
            where matches.Count > 0
            select acct.ID;
for (int i; i < accts.Count() - 1; i++)
{
    filter += string.Format("IDAccount = {0} OR ",accts.ElementAt(i));
}
filter += string.Format("IDAccount = {0}",accts.Last());
dataGridView1.Filter = filter;

This works, but is cumbersome. I'd rather do it through bindings if there's a way.


Check out DataView class. It allows you to sort and filter records and also perform simple SQL-like manipulations.


I don't believe there is a way to do this directly with data bindings. I posted the solution as an edit in response to @CodeBlend's comments.

0

精彩评论

暂无评论...
验证码 换一张
取 消