0

I have a DataTable bound to a dataGridView using the following code :

            dataGridView1.DataSource = ReducedColTable;

At some point i would like to move the selected rows into another Table. So here is the loop :

   List<DataGridViewRow> rows = (from DataGridViewRow row in dataGridView1.SelectedRows
                                      where !row.IsNewRow
                                      orderby row.Index
                                      select row).ToList<DataGridViewRow>();
        foreach (DataGridViewRow item in rows)
        {
            DataRow newRow = dtarget.Rows.Add();
            newRow.SetField("artiste", item.Cells[0].Value);
            newRow.SetField("album", item.Cells[1].Value);
            newRow.SetField("song", item.Cells[2].Value);
            newRow.SetField("year", item.Cells[3].Value);
            newRow.SetField("file_path", item.Cells[4].Value);
            newRow.SetField("file_size", item.Cells[5].Value);
            dataGridView1.Rows.RemoveAt(item.Index);
        }

Ps : I didnt find a way to copy the row into the new table (dtarget) which is declared globally hence the approach col by col.

DataTable dtarget = new DataTable();

My issue is that the last line is removing the row from the DataGridView but not from the original table (ReducedColTable)

Any hint how to achieve this ?

4
  • Remove the record (Row) from the DataSource (your DataTable). If you need a DataTable identical to the original, then Clone() the original. You can get a DataRow object and copy it to the cloned DataTable. Commented Jul 9, 2020 at 20:45
  • well, what i want is to be able to move rows from the original to the target, so the clone wouldnt work right ? Commented Jul 9, 2020 at 20:51
  • 1
    You clone a DataTable to have an object with the same structure (and constraint). Then you can copy/move a DataRow from the original DT to the cloned when you need to. Not related to removing a DataRow, you still need to remove it from the DataSource, not the Control. Commented Jul 9, 2020 at 20:56
  • ok get it thanks, i'll try this to avoid filling the target dt col by col. thanks Commented Jul 9, 2020 at 21:21

1 Answer 1

1

I found that the DataTable’s ImportRow works well for this. If you set the grids SelectionMode to FullRowSelect then you should be able to loop through the grids SelectedRows collection and “import” the selected row(s) into the other DataTable. Below is a simple example.

dt and dt2 are two DataTables with similar schemas. dt is a data source for datagridview1 and dt2 is a data source to datagridview2. Initially, dt is filled with some data and dt2 is empty. Once the user selects one or more rows, a button's click event initiates moving the selected rows from dt to dt2.

To start, a simple loop through the selected rows. A check if the “new row” is selected which we do not want to copy, therefore we ignore it. Next, get the “data bound” row from the data table in the form of a DataRowView object. Then we “import” that row into dt2, and finally removing the copied row from dt. I did not do a lot of testing on this however it appears to work as expected.

private void button1_Click(object sender, EventArgs e) {
  DataRowView drv;
  foreach (DataGridViewRow row in dataGridView1.SelectedRows) {
    if (!row.IsNewRow) {
      drv = (DataRowView)row.DataBoundItem;
      dt2.ImportRow(drv.Row);
      dt.Rows.Remove(drv.Row);
    }
  } 
}

To make the example complete, drop two grids and button onto a form.

DataTable dt;
DataTable dt2;

public Form1() {
  InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e) {
  dt = GetTable();
  dt2 = GetTable();
  FillTable(dt);
  dataGridView1.DataSource = dt;
  dataGridView2.DataSource = dt2;
}

private DataTable GetTable() {
  DataTable dt = new DataTable();
  dt.Columns.Add("Col1", typeof(string));
  dt.Columns.Add("Col2", typeof(string));
  dt.Columns.Add("Col3", typeof(string));
  return dt;
}

private void FillTable(DataTable dt) {
  for (int i = 0; i < 10; i++) {
    dt.Rows.Add("C0R" + i, "C1R" + i, "C2R" + i);
  }
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.