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();
}