在Visual C#中将选定的行从DataGridView移动到另一个

时间:2016-01-07 16:51:38

标签: c# winforms datagridview rows move

我创建了一个包含两个Datagridviews的表单。其中一个DataGridViews填充了数据库中的数据,它是源。使用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中删除最后选择的行。什么是删除此选定行的最佳方法?

2 个答案:

答案 0 :(得分:0)

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

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();
}

答案 1 :(得分:0)

如果您想要&#34;移动&#34;(添加到目标并从源中删除)所选行到另一个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);
    }
}
相关问题