【问题标题】:Select a DataGridView row after TextBox value C#在 TextBox 值 C# 之后选择 DataGridView 行
【发布时间】:2011-11-10 00:32:31
【问题描述】:

我有一个带有 1 列和一些行的数据网格视图。我想做:
当用户在 TextBox 中写入值时,如果该值已存在于 datagridview 中,我想选择包含该 TextInput 值的行

如何做到这一点?
我打算这样使用:

dataGridView1.CurrentCell = dataGridView1[0, index];

但我不知道如何使用 TextBox 值查找索引。

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    您可以遍历行,直到找到与文本框的值匹配的行:

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        // Test if the first column of the current row equals
        // the value in the text box
        if ((string)row.Cells[0].Value == textBox1.Text)
        {
            // we have a match
            row.Selected = true;
        }
        else
        {
            row.Selected = false;
        }
    }
    

    【讨论】:

      【解决方案2】:

      试试这个方法:

       private void button1_Click(object sender, EventArgs e)
          {
              for (int i = 0; i < dataGridView1.Rows.Count; i++)
              {
                  if (!dataGridView1.Rows[i].IsNewRow)
                  {
                      if (dataGridView1[0, i].Value.ToString() == textBox1.Text)
                          dataGridView1.Rows[i].Selected = true;
                      else
                          dataGridView1.Rows[i].Selected = false;
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2016-10-08
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-12
        相关资源
        最近更新 更多