如何使 textBox 值等于 datagridview 单元格值
How can I make a textBox value equal to a datagrivew cell value
我有一个 winform 和 6 个文本框,当我按下按钮 1 时,每个文本框都会填充相应的 datagridview 单元格值。当我在 textbox6 中输入值后按下 button2 时,如何让标签显示 5 个文本框中任何一个的匹配值。我下面的代码不起作用。谢谢。
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";
}
您可以遍历除 #6 之外的所有文本框并比较文本框 6 的文本:
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;
}
}
如果您的表单中有更多文本框,您也需要将它们排除在外,以防止循环也对它们进行比较。您可以通过将它们放在类似的面板中来实现。
我有一个 winform 和 6 个文本框,当我按下按钮 1 时,每个文本框都会填充相应的 datagridview 单元格值。当我在 textbox6 中输入值后按下 button2 时,如何让标签显示 5 个文本框中任何一个的匹配值。我下面的代码不起作用。谢谢。
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";
}
您可以遍历除 #6 之外的所有文本框并比较文本框 6 的文本:
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;
}
}
如果您的表单中有更多文本框,您也需要将它们排除在外,以防止循环也对它们进行比较。您可以通过将它们放在类似的面板中来实现。