【问题标题】:Move selected row from DataGridView to another in Visual C#在 Visual C# 中将选定行从 DataGridView 移动到另一个
【发布时间】:2016-01-07 16:51:38
【问题描述】:

我创建了一个包含两个 Datagridview 的表单。其中一个 DataGridView 填充了来自数据库的数据,它是源。在我使用 Button 后,第二个 DataGridView 应该填充来自源 Datagridview 的选定行。

如果填充我的 DataTable 的第一步是这样填充的:

public DataTable loadMatImpTable(String query)
{
   myConn.Open();
   SQLiteCommand cmd = new SQLiteCommand(query, myConn);
   SQLiteDataAdapter sda = new SQLiteDataAdapter();
   sda.SelectCommand = cmd;
   DataTable dt= new DataTable();
   sda.Fill(dt);
   return dt;
} 

之后我填充了我的源 DataGridView:

DataTable dt = lt.loadMatImpTable(queryMat);

this.matExpDataGridVW.Rows.Clear();

foreach (DataRow item in dt.Rows)
{
    int n = matExpDataGridVW.Rows.Add();
    matExpDataGridVW.Rows[n].Cells[0].Value = false;
    matExpDataGridVW.Rows[n].Cells[1].Value = item["MaterialID"].ToString();
    matExpDataGridVW.Rows[n].Cells[2].Value = item["Name"].ToString();
    matExpDataGridVW.Rows[n].Cells[3].Value = item["Preis"];
    matExpDataGridVW.Rows[n].Cells[4].Value = item["Anzahl"].ToString();
    matExpDataGridVW.Rows[n].Cells[5].Value = item["Datum"].ToString();                
}

然后我按下“ExportButton”并将所有选定行复制到第二个 DatagRidview。但我不会复制第二个 DataGridview 中的行。我将移动选定的行。所以我尝试在 foreach 循环中删除项目:

private void mvImpSelectionBT_Click(object sender, EventArgs e)
 {
     foreach (DataGridViewRow item in matExpDataGridVW.Rows)
     {
        if ((bool)item.Cells[0].Value == true)
        {
            int n = matImpDataGridVW.Rows.Add();
            matImpDataGridVW.Rows[n].Cells[0].Value = false;
            matImpDataGridVW.Rows[n].Cells[1].Value = item.Cells[1].Value.ToString();
            matImpDataGridVW.Rows[n].Cells[2].Value = item.Cells[2].Value.ToString();
            matImpDataGridVW.Rows[n].Cells[3].Value = item.Cells[3].Value.ToString();
            matImpDataGridVW.Rows[n].Cells[4].Value = item.Cells[4].Value.ToString();
            matImpDataGridVW.Rows[n].Cells[5].Value = item.Cells[5].Value.ToString();
        }                
    }
    #Delete all Selected rows
    foreach (DataGridViewRow item in matExpDataGridVW.SelectedRows)
    {
        matExpDataGridVW.Rows.Remove(item);
    }
} 

但是如果我尝试以这种方式删除。复制所有选定的行,但仅删除源 DatGridView 中最后选定的行。删除此选定行的最佳方法是什么?

【问题讨论】:

    标签: c# winforms datagridview rows move


    【解决方案1】:

    在复制时创建行列表,然后使用该列表作为从源中删除行的基础。

    private void mvImpSelectionBT_Click(object sender, EventArgs e)
     {
         List<DataRow> rowsToDelete = new List<DataRow>();
         foreach (DataGridViewRow item in matExpDataGridVW.Rows)
         {
            if ((bool)item.Cells[0].Value == true)
            {
                //copy row
                int n = matImpDataGridVW.Rows.Add();
                matImpDataGridVW.Rows[n].Cells[0].Value = false;
                matImpDataGridVW.Rows[n].Cells[1].Value = item.Cells[1].Value.ToString();
                matImpDataGridVW.Rows[n].Cells[2].Value = item.Cells[2].Value.ToString();
                matImpDataGridVW.Rows[n].Cells[3].Value = item.Cells[3].Value.ToString();
                matImpDataGridVW.Rows[n].Cells[4].Value = item.Cells[4].Value.ToString();
                matImpDataGridVW.Rows[n].Cells[5].Value = item.Cells[5].Value.ToString();
    
                //add to list
                rowsToDelete.Add(item);
            }                
        }
        foreach(DataRow row in rowsToDelete)
        {
            matExpDataGridVW.Rows.Remove(row);
        }
    } 
    

    另一种只有一个循环的方法是使用带有迭代器的循环而不是 foreach 循环。

    for (int i = matExpDataGridVW.Rows.Count - 1; i >= 0; i--) 
    {
         if ((bool)matExpDataGridVW.Rows[i].Cells[0].Value == true)
            {
                //copy row
                int n = matImpDataGridVW.Rows.Add();
                matImpDataGridVW.Rows[n].Cells[0].Value = false;
                matImpDataGridVW.Rows[n].Cells[1].Value = matExpDataGridVW.Rows[i].Cells[1].Value.ToString();
                matImpDataGridVW.Rows[n].Cells[2].Value = matExpDataGridVW.Rows[i].Cells[2].Value.ToString();
                matImpDataGridVW.Rows[n].Cells[3].Value = matExpDataGridVW.Rows[i].Cells[3].Value.ToString();
                matImpDataGridVW.Rows[n].Cells[4].Value = matExpDataGridVW.Rows[i].Cells[4].Value.ToString();
                matImpDataGridVW.Rows[n].Cells[5].Value = matExpDataGridVW.Rows[i].Cells[5].Value.ToString();
    
                //delete row
                matExpDataGridVW.Rows[i].Delete();
         }
         matExpDataGridVW.AcceptChanges();
    }
    

    【讨论】:

      【解决方案2】:

      如果您想“移动”(添加到目标并从源中删除)选定的行到另一个 DataGridView。尝试使用DataSources移动项目

      将数据加载到原始DataGridView

      private DataTable _OriginalData;
      private DataTable _SelectedData;
      
      private void LoadData()
      {
          string yourQuery = "SELECT ... FROM ...";
          _OriginalData = loadMatImpTable(yourQuery);
          //copy structure of original DataTable to the selected table
          _SelectedData = _OriginalData.Clone();
          //Fill DataGridView
          this.matExpDataGridVW.DataSource = _OriginalData;
          this.matImpDataGridVW.DataSource = _SelectedData;
          //Columns will be generate automatically
          //If you want use predefined columns create them through designer or with code
          //And set then DataGridView.AutoGenerateColumns = false;
      }
      

      那么导出的物品会是这样的

      private void ExportSelectedRows()
      {
          foreach DataRow row in matExpDataGridVW.SelectedRows
                                                 .Cast<DataGridViewRow>()
                                                 .Select(r => r.DataBoundItem as DataRowView)
                                                 .Where(drv => drv != null)
                                                 .Select(drv => drv.Row)
          {
              _SelectedData.ImportRow(row);
              _OriginalData.Rows.Remove(row);
          }
      }
      

      【讨论】:

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