将多个选定的行从datagridview复制到文本框

时间:2016-03-31 07:05:17

标签: c# datagridview

我有一个名为PTable的datagridview,它显示了一个来自数据库的表。我还有一个按钮,用于将所选行的数据复制到我拥有的文本框中。

我现在拥有的这段代码会从datagridview中复制两个选定的行,但是当我只选择一行或选择超过2行时,它会说"索引超出范围"

应该发生的是它应该复制任意数量的行而不仅仅是2行

private void button11_Click(object sender, EventArgs e)
    {

            productid.Text = PTable.SelectedRows[0].Cells[0].Value + string.Empty;
            productname.Text = PTable.SelectedRows[0].Cells[1].Value + string.Empty;
            unitprice.Text = PTable.SelectedRows[0].Cells[4].Value + string.Empty;
            productid2.Text = PTable.SelectedRows[1].Cells[0].Value + string.Empty;
            productname2.Text = PTable.SelectedRows[1].Cells[1].Value + string.Empty;
            unitprice2.Text = PTable.SelectedRows[1].Cells[4].Value + string.Empty;

    }

3 个答案:

答案 0 :(得分:0)

如果用户未选择两行,则索引1(PTable.SelectedRows [1])无效,因为该项不存在。 在运行此代码之前,您必须检查用户是否选择了两行:

if (PTable.SelectedRows.Count == 2)
{
  //Your code ...
}
else
{
  //Not two rows selected
}

答案 1 :(得分:0)

首先,确保DataGridView的 SelectionMode FullRowSelect (希望您已经完成)

第二次,使用 foreach 或任何循环逻辑来浏览每个选定的行。

第三,编写模块化代码总是更好。编写一个验证方法,根据网格行的选择返回TRUE或FALSE。 现在基于返回值,您需要继续编写业务逻辑。

第四,请务必使用 NULL 检查

所以让我们从重新分解代码开始。

  private void button11_Click(object sender, EventArgs e)
  {
      If(IsRowOrCellSelected())
      {
           //loop through selected rows and pick your values.
      }       
  }

我只是在写样本,确保可以访问PTable。

  private boolean IsRowOrCellSelected()
  {
    if (PTable.SelectedRows.Count > 0)
    {
        DataGridViewRow currentRow = PTable.SelectedRows[0];
        if (currentRow.Cells.Count > 0) 
        {      
            bool rowIsEmpty = true;    

            foreach(DataGridViewCell cell in currentRow.Cells)    
            {
               if(cell.Value != null) 
               { 
                  rowIsEmpty = false;
                  break;
               }    
            }
       }
      if(rowIsEmpty)
           return false;
      else
          return true;
   }

代码仍然可以改进。

答案 2 :(得分:0)

要处理不同数量的选定行,我建议如下。

我的方法是:

您必须显示的行数可能超过5行。首先为TextBoxes创建3个面板。我已将productids的名称命名为 here 。从代码生成文本框,这样可以更容易地维护超过5个选定的行:

// This code goes to the constructor of the class. Not in the button click
List<TextBox> productids = new List<TextBox>();
List<TextBox> productnames = new List<TextBox>();
List<TextBox> unitprices = new List<TextBox>();
for (int i = 0; i < 5; i++)
{
    productids.Add(new TextBox { Top = i * 32 });
    productnames.Add(new TextBox { Top = i * 32 });
    unitprices.Add(new TextBox { Top = i * 32 });
    here.Controls.Add(productids[i]);
    here2.Controls.Add(productnames[i]);
    here3.Controls.Add(unitprices[i]);
}

比您可以在按钮单击中设置每个选定行的值:

// This code goes to the button click
// first empty the Textboxes:
foreach (TextBox productid in productids)
{
    productid.Text = string.Empty;
}
foreach (TextBox productname in productnames)
{
    productname.Text = string.Empty;
}
foreach (TextBox unitprice in unitprices)
{
    unitprice.Text = string.Empty;
}

for (int i = 0; i < PTable.SelectedRows.Count; i++)
{
    productids[i].Text = PTable.SelectedRows[i].Cells[0].Value.ToString();
    productnames[i].Text = PTable.SelectedRows[i].Cells[1].Value.ToString();
    // or better... Name the columns
    // unitprices[i].Text = PTable.SelectedRows[i].Cells["unitPrices"].Value.ToString();
    unitprices[i].Text = PTable.SelectedRows[i].Cells[4].Value.ToString();
}
相关问题