2016-01-07 51 views
1

我创建了一个包含两个Datagridviews的表单。其中一个DataGridView充满了来自数据库的数据,它是源代码。在使用Button之后,第二个DataGridView应该使用源Datagridview中的选定行填充。将选定的行从DataGridView移动到另一个Visual C#

的第一步,如果填补我的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中被删除。什么是删除这些选定行的最佳方法?

回答

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

如果你想“招”(添加到目的地,并从源中删除)选定的行到另一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); 
    } 
} 
相关问题