DataGridView Datatable Copy-Keep-Add-Update rows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • opendoor8
    New Member
    • Jun 2015
    • 1

    DataGridView Datatable Copy-Keep-Add-Update rows

    Hi,

    I have a datagridview with records completed by datatable dt:
    This datatable has 20 columns with 1 identity column

    dataGridView1.D ataSource = dt;


    I need to solve next problems:
    1. Select rows filtered by column[2]
    2. Keep these records
    3. Add exactly the same records below but update column[2] with different value (so we’ll have twice more records)

    I used next code for copying from one datatable to another but stack how to add/update rows


    Code:
    DataTable dtSource = ((DataTable)dataGridView1.DataSource);
    
    DataTable dtTarget = new DataTable();
    dtTarget = ((DataTable)dataGridView1.DataSource).Clone();
    
    DataRow[] rowsToCopy;
    
    rowsToCopy = ((DataTable)dataGridView1.DataSource).Select("DrawingNo='DM-3012'
     ");
    
    foreach (DataRow temp in rowsToCopy)
    {
    	dtTarget.ImportRow(temp);
    }
    
    dt = dtTarget;

    Thanks,
    Last edited by Rabbit; Jun 13 '15, 12:59 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
Working...