Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I have a user control with a checkedListBox and one button. I show this fill this popup on dataGridView1_CellDoubleClick event (I'll post code below) on popup button click I want to use bindingSource.Filter.

I tried it from this user control button_click event but it was doing nothing then I made a method in the form where there is the datagridview and still nothing. So I need help on how to get column name and than make method which can filter bindingSource.

Please help me. I am using C# 3.5 WinForms

This is the user control button code:

C#
private void button1_Click(object sender, EventArgs e)
    {
        OperatorsForm of = new OperatorsForm();
        var checkedvalues = checkedListBox1.CheckedItems.Cast<string>().Select(s => "'" + s + "%'").ToArray();
        string  d = "LoadName LIKE " + string.Join(" or ", checkedvalues);
        of.filter(d);
    }

This is popup code :
private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
    {
        UserControl1 ucontrol = new UserControl1();
        var button = sender as DataGridView;

        HashSet<string> strings = new HashSet<string>();
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            strings.Add(dataGridView1.Rows[i].Cells[e.ColumnIndex].Value.ToString());
        }

        checkedListBox1.Items.AddRange(strings.ToArray());
        ucontrol.FillList(strings.ToList());

        var listViewItems = strings.Select(x => new ListViewItem(x, 0)).ToArray();

        ListView listView = new ListView();
        listView.View = View.SmallIcon;
        listView.MultiSelect = false;

        listView.Items.AddRange(listViewItems);

        int itemToShow = 18;
        var lastItemToShow = listViewItems.Take(itemToShow).Last();
        int height = lastItemToShow.Bounds.Bottom + listView.Margin.Top;
        listView.Height = height;
        var popup = new ToolStripDropDown();
        popup.AutoSize = false;
        popup.Margin = Padding.Empty;
        popup.Padding = Padding.Empty;
        ToolStripControlHost host = new ToolStripControlHost(ucontrol);
        host.Margin = Padding.Empty;
        host.Padding = Padding.Empty;
        host.AutoSize = false;
        host.Size = ucontrol.Size;
        popup.Size = ucontrol.Size;
        popup.Items.Add(host);

        popup.Show(this, dataGridView1.Top, dataGridView1.Top);
    }
Posted
Updated 23-Nov-10 9:39am
v2
Comments
Henry Minute 23-Nov-10 17:47pm    
I have modified my answer to take account of your comment. Good luck. :)

1 solution

Where I think that your problem lies is in the button1_Click event handler

Your Code:
C#
private void button1_Click(object sender, EventArgs e)
    {
        OperatorsForm of = new OperatorsForm(); //<==== #1 =========
        var checkedvalues = checkedListBox1.CheckedItems.Cast<string>().Select(s => "'" + s + "%'").ToArray();
        string  d = "LoadName LIKE " + string.Join(" or ", checkedvalues);
        of.filter(d);  //<=========== #2 =================
    }
</string>


I have numbered the problem lines.

1. This creates a new instance of the Form, it does not get a reference to the existing Form. Modify it as follows:
C#
OperatorsForm of = this.FindForm() as OperatorsForm

2. After deleting line 1. of no longer exists. What you do now depends on whether you have a filter(string filterString) method(a.) or not (b.).
a. use this.filter(d);
b. use this.myBindingSource.Filter = d;

You should now be able to use of.filter(d); as before.

I hope that I have understood your problem and that this helps.

Good luck. :)
 
Share this answer
 
v2
Comments
lester555 23-Nov-10 17:09pm    
thanks henry but this is not in operatorsFor it is in user control

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900