在数据绑定组合框中搜索? [英] Searching in databound comboBox?
问题描述
我有一个组合框配置工作像dropdownlist。它正在通过数据库填充。
现在我想做的就是这样:
例如,我有comboBox从数据库表中获取Names列 tblPersons并显示它。
if,例如,在数据库表tblPersons中共有4个条目
- Ali Khan
-
-
-
-
- Bilal Farooqi
所以当我在comboBox中写入Ahmed Farooqi(comboBox应该是可编辑的,一旦我写了Ahmed,它就会显示所有名称为Ahmed的条目),然后按回车键,表tblPersons - 如果找到Ahmed Farooqi - 应该选择它,如果没有找到Ahmed Farooqi - 它应该问我是否要在数据库表tblPersons中创建新条目,如果我单击是它应该打开另一个表单,以添加新的人,如果我单击否,它不应该做任何事。
我应该如何做。我是新的C#,所以任何帮助将非常感谢。
编辑:这是我的测试表单,这里是我的测试表单将用于输入名称的文本框,文本框应在数据库中搜索名称,然后如果找到名称,则在数据库的相应文本框中显示数字和电子邮件。如果没有找到,它应该显示messagebox要求用户创建一个新的人,如果点击是,切换到另一种形式(用户将创建新的人)。
调用表单中的fillCombo函数加载并将cmb_SelectedValueChanged事件分配给组合框。不要忘记将autocompletemode属性设置为combobox。
cmb.AutoCompleteMode = AutoCompleteMode.SuggestAppend
private void fillCombo()
{
string sql =SELECT ID,NAME,Number,Email FROM tblPerson;
SqlConnection Cnn = new SqlConnection(connections string);
Cnn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql,Cnn);
DataTable dt = new DataTable();
da.Fill(dt);
cmb.ValueMember =ID;
cmb.DisplayMember =Name;
cmb.DataSource = dt;
}
private void cmb_SelectedValueChanged(object sender,EventArgs e)
{
if(cmb.SelectedItem!= null)
{
txtNumber.Text =((DataRow)cmb.SelectedItem)[Number]。ToString();
txtEmail.Text =((DataRow)cmb.SelectedItem)[Email]。ToString();
}
}
private void cmb_Validating(object sender,CancelEventArgs e)
{
if(cmb.SelectedItem == null)
{
If(MessageBox.Show(Do you want to create a new person?,MessageBoxButtons.YesNo)== DialogResult.Yes)
{
frmNewPerson person = new frmNewPerson
person.Name = cmb.Text;
if(person.ShowDialog()== DialogResult.Yes)
{
fillCombo();
DataRow dr =((DataTable)cmb.DataSource).Select(Name ='+ person.Name +')[0];
cmb.SelectedValue = Convert.ToInt32(dr [ID]);
}
}
else
{
txtNumber.Text = string.Emtpy;
txtEmail.Text = string.Emtpy;
}
}
}
Guys I have a comboBox that is configured to work like dropdownlist. It is being populated through database. It is working flawlessly.
Now what I want to do is is this : For example I have comboBox that is getting "Names" column from database table "tblPersons" and displaying it. if, For example, there are total of 4 entries in database table "tblPersons"
- Ali Khan
- Ahmed Khan
- Bilal Khan
- Bilal Farooqi
So when I write "Ahmed Farooqi" in comboBox (comboBox should be editable and it should display all entries with name "Ahmed" once I have written "Ahmed") and press enter, it should search in database table "tblPersons" - if "Ahmed Farooqi" is found - it should select it, if "Ahmed Farooqi" is not found - it should ask if I want to create new entry in database table "tblPersons", if I click "yes" it should open another form for adding new person and if I click "No" it should do nothing.
How should I do this. I am new to C#, so any help would be greatly appreciated. I apologize if my question was unclear.
Regards.
Edit : This is my test form, here I a textBox that will be used to enter Name, the textbox should search for a name in database, then if name is found, display number and email from database in their respective textboxes. If not found, it should display messagebox asking user to create a new person and if clicked yes, switch to another form (where user will create new person).
call the fillCombo function in your form load and assign the cmb_SelectedValueChanged event to your combobox. Don't forget to set autocompletemode property to combobox.
cmb.AutoCompleteMode = AutoCompleteMode.SuggestAppend
private void fillCombo()
{
string sql = "SELECT ID, NAME, Number, Email FROM tblPerson";
SqlConnection Cnn = new SqlConnection("connections string");
Cnn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, Cnn);
DataTable dt = new DataTable();
da.Fill(dt);
cmb.ValueMember = "ID";
cmb.DisplayMember = "Name";
cmb.DataSource = dt;
}
private void cmb_SelectedValueChanged(object sender, EventArgs e)
{
if (cmb.SelectedItem != null)
{
txtNumber.Text = ((DataRow)cmb.SelectedItem)["Number"].ToString();
txtEmail.Text = ((DataRow)cmb.SelectedItem)["Email"].ToString();
}
}
private void cmb_Validating(object sender, CancelEventArgs e)
{
if (cmb.SelectedItem == null)
{
If (MessageBox.Show("Do you want to create a new person?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
frmNewPerson person = new frmNewPerson();
person.Name = cmb.Text;
if (person.ShowDialog() == DialogResult.Yes)
{
fillCombo();
DataRow dr = ((DataTable)cmb.DataSource).Select("Name='" + person.Name + "'")[0];
cmb.SelectedValue = Convert.ToInt32(dr["ID"]);
}
}
else
{
txtNumber.Text = string.Emtpy;
txtEmail.Text = string.Emtpy;
}
}
}
这篇关于在数据绑定组合框中搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!