I have a winform and 6 textboxes, when I press button1 each textboxes get populated with their corresponding datagridview cell values. When I press button2 after entering a value in textbox6 how can I make a label show the matching value of any of the 5 textboxes. My code below doesn't work. Thank you.
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
dataGridView1.DataSource = dt;
textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
textBox2.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
textBox3.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
textBox4.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
textBox5.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text.Equals(textBox2.Text))
{
label1.Text = "this is equal to textBox1";
}
You could loop through all textboxes but #6 and compare the text of your textBox6:
foreach (TextBox control in Controls) //all textboxes but textbox6, because you dont want to compare it with itself
{
if (control.Text.Equals(textBox6.Text) && textBox6 != control)
{
label1.Text = control.Text;
}
}
If you have more textboxes in your form you need to exclude them as well to prevent the loop from comparing them too. You can achive this by putting them in a panel of something similar.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments