2个文本框我只获得一个选定行的值 [英] 2 text boxes i am getting value of only one selected row

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

问题描述

想要在2个文本框中获取所选行的单元格0的值。有两个选定的行但在两个文本框中我只获得一个选定行的值。帮助我,THANX

want to get value of cell 0 of selected rows in 2 text boxes..there are 2 selected rows but in both text boxes i am getting value of only one selected row. 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;
                  int j = dataGridView1.SelectedCells[0].RowIndex;
                  string str = dataGridView1.Rows[j].Cells[0].Value.ToString();

                  string[] gcnic;
                  gcnic = str.Split('-');
                  txtcnic1.Text = gcnic[0];
                  txtcnic2.Text = gcnic[1];
                  txtcnic3.Text = gcnic[2];

                  str = dataGridView1.Rows[j].Cells[0].Value.ToString();


                  gcnic = str.Split('-');
                  txtgcnic1.Text = gcnic[0];
                  txtgcnic2.Text = gcnic[1];
                  txtgcnic3.Text = gcnic[2];
              }

推荐答案

想要获取2个文本框中所选行的单元格0的值

您的代码几乎完全相反。您正在将单元格值与文本框文本进行比较并执行某些操作。



您需要执行以下操作:

want to get value of cell 0 of selected rows in 2 text boxes
Your code does almost opposite. You are comparing cell value with textbox text and doing some operation.

You need to do something like:
for(i=0;i<=dataGridView1.Rows.Count-1;i++)
{
  if (dataGridView1.Rows[i].Selected)
  {
     // selected row cell[0] concatenated value. 
     string str = dataGridView1.Rows[i].Cells[0].Value.ToString() + '-' + str;
  }
}


您正在将所有文本框设置为循环中的一行。

循环第一个选定的行,所有文本框都设置为此行的值。

当下一个选定的行循环播放时,所有文本框都设置为第二行的值。



因此,您将获得第二个选定行的所有值。
You are setting all the textboxes to one row in the loop.
When the first selected row is looped over, all textboxes are set to values from this row.
When the next selected row is looped over, all textboxes are set to values from this second one.

As a result, you get all values from the second selected row.


这篇关于2个文本框我只获得一个选定行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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