选择包含文本框值的gridview行 [英] select gridview rows which contains text box values

查看:103
本文介绍了选择包含文本框值的gridview行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择包含文本框值的gridview行,有两行包含文本框值。但是代码只选择了一行......两者都应该选中。帮助我。 ThanX

i want to select gridview rows which contains text box values, there are two rows that contains text box value. but the code is selecting only one row..both should selected. help me with this. ThanX

bool itemFound = false;
        for(i=0;i<=dataGridView1.Rows.Count-1;i++)
          {
              if (dataGridView1.Rows[i].Cells[1].Value.ToString() == txtcscnic1.Text + "-" + txtcscnic2.Text + "-" + txtcscnic3.Text)
              {

                  dataGridView1.Rows[i].Selected = true;
                  itemFound = true;
                  break;

              }

          }

          if (!itemFound)
          {
              MessageBox.Show("Customer with CNIC'" + txtcscnic1.Text + "-" + txtcscnic2.Text + "-" + txtcscnic3.Text + "' does not Exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }

推荐答案

您需要确保DataGridView的MultiSelect属性设置为true - 它是默认情况下,如果您将其设置为false,它将显示您描述的行为。



错字:MultiSelect的MultiSecelt - OriginalGriff [/ edit]
You need to make sure that the MultiSelect property of the DataGridView is set to "true" - it is by default, but if you have set it to "false" it will exhibit the behaviour you describe.

[edit]Typo: "MultiSecelt" for "MultiSelect" - OriginalGriff[/edit]


像这样更新你的代码

Update your code like this
bool itemFound = false;
for(i=0;i<=dataGridView1.Rows.Count-1;i++)
{
  if (dataGridView1.Rows[i].Cells[1].Value.ToString() == txtcscnic1.Text + "-" + txtcscnic2.Text + "-" + txtcscnic3.Text)
   {
      dataGridView1.Rows[i].Selected = true;
      itemFound = true;  
      //break; Commented this line
    }
 }

if (!itemFound)
{
   MessageBox.Show("Customer with CNIC'" + txtcscnic1.Text + "-" + txtcscnic2.Text + "-" + txtcscnic3.Text + "' does not Exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}


这篇关于选择包含文本框值的gridview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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