ehsano2

hi

I have a table in my database with 2 fields( f1,f2) and a form with a combobox and a textbox

I want to select f2 from table where the f1 like textbox.text and fill the combobox with the f2 that is selected.

I want when the user is typing the text in textbox the select operation happens with out any buttons.

please give me the code completely for C#

thanks



Re: Windows Forms Data Controls and Databinding fill the combobox with a field

Tamirro

You can handle the Validate event of the TextBox or the TextChanged

and for every text been written find the right record and give the ComboBox.SelectedText or SelectedIndex

the f2





Re: Windows Forms Data Controls and Databinding fill the combobox with a field

Zhi-Xin Ye - MSFT

You can use a BindingSource as DataSource for the TextBox and ComboBox, handle the TextChanged event of TextBox, A sample code for you

Code Snippet

public partial class Form12 : Form

{

public Form12()

{

InitializeComponent();

}

private void Form12_Load(object sender, EventArgs e)

{

dt = new DataTable();

dt.Columns.Add("f1");

dt.Columns.Add("f2");

for (int j = 0; j < 10; j++)

{

dt.Rows.Add("f1" + j.ToString(), "f2" + j.ToString());

}

this.bs = new BindingSource();

this.bs.DataSource = dt;

this.textBox1.DataBindings.Add("Text", this.bs, "f1");

this.comboBox1.DisplayMember = "f2";

this.comboBox1.ValueMember = "f1";

this.comboBox1.DataSource = this.bs;

this.textBox1.TextChanged += new EventHandler(textBox1_TextChanged);

}

BindingSource bs;

DataTable dt;

void textBox1_TextChanged(object sender, EventArgs e)

{

int pos = this.bs.Find("f1", this.textBox1.Text);

if (pos > -1)

{

this.bs.Position = pos;

}

}

}