Click here to Skip to main content
15,910,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am creating a Multicolumn comboBox with ToolStripDropDown control and a Gridview. I want a feature when user input a text in the combo box, gridview control should filtered with the input text match with them in any where.

Example: combo box with 2 items: "test", "this is a test".
-> when user input "test" in combo box, both of items are suggested (default feature only lists the first item).

So, I must implement the event TextChanged of combo box, and a popup window will list the matched items (based on my search feature). However, when popup window is displayed, the combo box will lost the focus, so I can't input more text to increase.

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a64dd06f-a55e-4a0c-b5e1-ce9fa9f5cca5[^]

Please guide me to solve this problem.
Thanks.
Posted
Updated 28-Apr-12 1:40am
v4
Comments
Ehsan Faramarzi 3-Jun-15 2:21am    
I got the exact problem. Have you got the solution? please help me! faramarzi@email.com

Please these article
MultiColumnComboBoxEx: An Extended Data-Bound Multiple Column ComboBox[^]
and this help page
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripdropdown.aspx[^]
they may be helpful.

But as seen from the question it appears that as the text is being typed in the ComboBox the DatagridView is filtered. I think there is no requirement of focus to be given to the DataGridView as it is being used only for the purpose of displaying the filtered data. In such case, use a TextBox with a DataGridView below it. When, the TextBox receives focus, show the DataGridView and when the TextBox loses focus hide the DataGridView. In the TextChanged event of TextBox, apply the filter to the BindingSource which is assigned to the DataSource property of the DataGridView. In this way the focus from the TextBox is not not lost, when the filter is applied to the DataGridView as shown below:
C#
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace GridViewComboxBox {
    public partial class Form1 : Form {
        DataTable DataTable1;
        BindingSource BindingSource1;
        DataGridView DataGridView1;
        TextBox TextBox1;
        Button Button1;
        
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            DataTable1 = new DataTable();
            DataTable1.Columns.Add("ItemNo", typeof(int), null);
            DataTable1.Columns.Add("ItemDescription", typeof(string), null);

            DataTable1.Rows.Add(1, "This is first Item");
            DataTable1.Rows.Add(2, "This is second Item");
            DataTable1.Rows.Add(3, "This is third");
            DataTable1.Rows.Add(4, "This is fourth");

            BindingSource1 = new BindingSource();
            BindingSource1.DataSource = DataTable1;
            DataGridView1 = new DataGridView();
            DataGridView1.DataSource = BindingSource1;

            TextBox1 = new TextBox();
            TextBox1.GotFocus += (s, args) => DataGridView1.Show();
            TextBox1.LostFocus += (s, args) => DataGridView1.Hide();
            TextBox1.TextChanged += (s, args) => 
                BindingSource1.Filter = string.Format(
                "ItemDescription like '%{0}%'", TextBox1.Text);

            TextBox1.Location = new Point(10, 10);
            TextBox1.Width = 275;
            Controls.Add(TextBox1);

            DataGridView1.Location = new Point(10, 15 + TextBox1.Location.Y);
            DataGridView1.Width = 275;
            Controls.Add(DataGridView1);

            Button1 = new Button();
            Button1.Text = "Button";
            Button1.Location = new Point(10, 50);
            Controls.Add(Button1);
        }
    }
}

To run the above sample, create a Windows Forms application and replace the contents of the Form1.cs file with the above code and run the application.


I think it may be helpful.
 
Share this answer
 
v4
Comments
georgegarvasis 28-Apr-12 7:11am    
Hi Reddy,
thanks for your reply
my question was not clear.i have corrected after your reply.
actually i am creating a multicolumn comboBox with datagridview.
georgegarvasis 28-Apr-12 10:17am    
i thanks for your valuable information.
i had tried your first link,it was ok,but i couldn't show the column header.
any way i will go through your information.
thanks again.
VJ Reddy 28-Apr-12 10:55am    
You can also try the sample code given in the solution.
Thank you.
Just add ToolStripDropDown.AutoClose = false;
and when the textChanged event of textbox fired, show the popup and call Textbox.Focus();
 
Share this answer
 
Comments
georgegarvasis 30-Apr-12 4:53am    
Thanks ngan,
i will try this.
georgegarvasis 1-May-12 7:47am    
thanks ngan for your valuable help.
now the control keeping focus while we show the Popup control.but popup control is not fully hiding after i called the Hide method.still a rectangular showing without the gridview.any idea about this.
thaks again.

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