【问题标题】:Row provided already belongs to a DataGridView control C#提供的行已经属于 DataGridView 控件 C#
【发布时间】:2014-04-01 09:51:04
【问题描述】:

您好,我正在尝试将 DatagridView 的行移动到 DataGridViewRowCollection,并且此集合表示 DataGridViewRowCollection 列表中名为“SacRowsColl”的项目。

现在我在 foreach 循环的第二行有一个 ArgumentOutOfRangeException,当我在它导致另一个异常“提供的行已经属于 DataGridView 控件”之前注释该行时。

     private void ChangeSac_DeselectingTabPage(object sender, TabControlCancelEventArgs e)
     {
        List<DataGridViewRowCollection> SacRowsColl = new List<DataGridViewRowCollection>();

        if (dgv.Rows.Count != 0)
        {
            foreach (DataGridViewRow row in dgv.Rows.OfType<DataGridViewRow>().ToArray())
            {
                dgv.Rows.Remove(row);
                SacRowsColl[0].Add(row);//this line throws an ArgumentOutOfRangeException
            }
        }
      }

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    无需一次又一次地重新添加行。行本身会跟踪它所在的 DataGridView。

    【讨论】:

      【解决方案2】:

      每次将新行添加到相同的 DataGridViewRowCollection SacRowsColl[0]

      你也应该增加SacRowsColl

      【讨论】:

      • 的列表
      • SacRowsColl[0] 是行的集合,它呢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 2013-12-03
      • 1970-01-01
      相关资源
      最近更新 更多