文本框在ontextchanged上填充 [英] textboxes populate ontextchanged

查看:89
本文介绍了文本框在ontextchanged上填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个代码示例,该代码示例将允许我在文本框中键入名称,并且如果存在记录,则在更改文本框时在数据库中将信息填充到文本框中.

请提供示例代码,请



I need a sample of code that will allow me to type a name in a text box and on change of the text box if record exist populate information from my database into text boxes.

sample code please, please, please



thanks ahead of time.

推荐答案

每次输入正确的员工姓名textbox1和textbox2都会显示其附加信息.
Every time you enter correct employee name textbox1 and textbox2 shows his additional information.
DataSet ds;        
        private void Form1_Load(object sender, EventArgs e)
        {
            ds = new DataSet();
            DataAdapter da = new SqlDataAdapter(@"SELECT * FROM Personel", @"Data Source=HEINZZ-PC\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True");
            da.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0];          
        }        

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            string text = ((ComboBox)sender).Text;
            IEnumerable<DataRow> query =
                from employee in ds.Tables[0].AsEnumerable()
                where (employee["Name"]).ToString().Trim() == text
                select employee;
            if (query.Count() != 0)
            {
                this.textBox1.Text = query.First()["ID"].ToString();
                this.textBox2.Text = query.First()["Age"].ToString();
            }
        }






这是实现它的方法之一.如果将组合框与数据源一起使用,它将提示用户正确的名称.






This is one of the ways to make it. If you use a combobox with datasource it will prompt user to the right name.

private void Form1_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            DataAdapter da = new SqlDataAdapter(@"SELECT * FROM Personel", @"Data Source=HEINZZ-PC\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True");
            da.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0];
            
            this.comboBox1.DataSource = ds.Tables[0];
            this.comboBox1.DisplayMember = "Name";
            
            this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.textBox1.Text = ((DataRowView)(this.comboBox1.SelectedItem))["ID"].ToString();
            this.textBox2.Text = ((DataRowView)(this.comboBox1.SelectedItem))["Age"].ToString();
        }


您需要研究 c#过滤数据集 c#过滤数据表,以了解如何获取正确的记录.

您还应该查询 c#参数化查询,以了解正确/安全的方法.
You need to research on c# filtering dataset or c# filtering datatable to find out how to get the right records.

You should also look up c# parameterized queries for the correct/safe way to do it.


这篇关于文本框在ontextchanged上填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆