【问题标题】:Copy multiple selected rows from datagridview to textboxes将多个选定的行从 datagridview 复制到文本框
【发布时间】:2016-03-31 07:05:17
【问题描述】:

我有一个名为 PTable 的数据网格视图,它显示数据库中的一个表。我还有一个按钮,可以将数据从选定的行复制到我拥有的文本框中。

我现在拥有的这段代码从 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;

    }

【问题讨论】:

  • 不需要+ string.Empty。删除它们。您有多少 productidx productnamex unitpricex 文本框?将它们放入列表中
  • 首先将DataGridView的SelectionMode改为FullRowSelect。否则用户可能会选择单元格而不是行,并且代码将不起作用
  • @ntohl 好的。我将删除 string.Empty。我在 datagridview 上有 3 行,但添加新产品时会更多。我有 5 个文本框。
  • 也使用代码进行 NULL 检查。
  • @codetoshare 已经在 FullRowSelect 中

标签: c# datagridview


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    首先,确保DataGridView的SelectionModeFullRowSelect(希望你已经做到了)

    第二,使用 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;
       }
    

    代码仍有待改进。

    【讨论】:

      【解决方案3】:

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

      我的做法是:

      您必须显示的行数可能超过 5 行。首先为文本框创建 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();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-15
        相关资源
        最近更新 更多