Georg Gutsche
Georg Gutsche

Reputation: 502

Move selected row from DataGridView to another in Visual C#

I created a form with two Datagridviews. One of these DataGridViews is filled with data from a Database and it is the source. The second DataGridView should be filled with the selected rows from the source Datagridview after I use a Button.

The first step if fill my DataTable is filled this :

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

After that I fill my source 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();                
}

And then I push the "ExportButton" and all Selected Rows are copied to the second DatagRidview. But I don't wont't a copy of the rows in the second DataGridview. I will move the selected rows. So I tried in a remove of the items in a foreach loop:

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

But if I tried the deletion in this way. All selected rows be copied but only the last selected row be deleted in the source DatGridView. Whats the best way to delete this selected rows?

Upvotes: 1

Views: 2009

Answers (2)

Fabio
Fabio

Reputation: 32453

If you want "move"(add to destination and remove from source) selected rows to another DataGridView. Try to move items by using DataSources

Load data to original 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;
}

Then export items will be like this

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

Upvotes: 0

devlin carnate
devlin carnate

Reputation: 8622

Create a list of rows while you are copying, and then use that list as a basis for deleting your rows from the source.

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

Another way to do this, with only one loop, is to use a loop with an iterator instead of a foreach loop.

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

Upvotes: 0

Related Questions