简体   繁体   中英

how to copy a column from one datagrid into another

Given two WPF data grids, would it be possible to copy the data from one Datagrid column into the other programmatically?

So all of the data in DataGrid A , columns[1] would be passed into Datagrid B , columns[2] .

So copying from DataGrid A (dialog.displayTable), to DataGrid B (just displayTable) I did it by iterating through DataGrid A row by row, and then copying the column data into a new row, column by column. Then adding that row to the DataGrid B.

In this example I also used a separate DataGrid with headers set to ComboBox's to re-map the columns if I so chose.

   foreach(DataRow displayRow in ((DataView)dialog.displayTable.ItemsSource).ToTable().Rows)
    {
        int i = 0;
        DataRow drNew = displayTable.NewRow();
        foreach (DataGridColumn selectedCol in dialog.SelectionTable.Columns)
        {

            drNew[(selectedCol.Header as ComboBox).SelectedIndex] = displayRow[i];
            i++;
        }
        displayTable.Rows.Add(drNew.ItemArray);
    }

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