简体   繁体   中英

delete a DataTable row bound to a datagridview

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM