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:
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.