Michael Lewis
Michael Lewis

Reputation: 147

C# How do I copy from one data grid to another without overwriting my previous selection?

I have 2 datagrid views with one datatable. I am trying to have a button that when clicked it adds the rows from csv_datagridview to optimal_datagridview. The below works however whenever I deselect an entry in csv_datagridview and hit the button again it clears that selection. I would like to have the selection stick each time.

  if (selectedRowCount <= 9)
                {


                    List<object> destList = new List<object>();
                    foreach (DataGridViewRow row in csv_datagridview.SelectedRows)
                        destList.Add(row.DataBoundItem);
                    optimaldataGridView.DataSource = destList;

enter image description here

enter image description here

Thank you so much in advance :)

Upvotes: 0

Views: 1674

Answers (2)

JohnG
JohnG

Reputation: 9469

It is unclear what your exact problem is with the little code you show, but from your statement whenever I deselect an entry in csv_datagridview and hit the button again it clears that selection. I am guessing that if nothing is selected, the data in optimaldataGridView clears when you press the add selected button.

I will assume the csv_datagridview is bound to a table. Your posted code shows the creation of new List destList which you fill with the selected rows from the csv_datagridview. Then you set optimaldataGridView data source to the destList. One issue I see in this picture is that as soon as you leave the if (selectedRowCount <= 9) clause… destList will no longer exist. As a data source for a DataGridView on your form, I would think you would want to keep this List global as long as the form is open. Either way... you are not adding the selected rows, you are simply removing the existing rows and then adding what was selected in csv_datagridview.

I hope the code below will help. I created two DataTables, one for each DataGridView. The csv_datagridview data table is filled with some data, the second data table is left empty. Then simply add the selected rows from the csv_datagridview to the optimaldataGridView’s DataTable… Then refresh optimaldataGridView.

DataTable table1;
DataTable table2;

public Form1() {
  InitializeComponent();
  csv_datagridview.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  optimaldataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  table1 = GetTable("table1");
  table2 = GetTable("table2");
  FillTable(table1);
  csv_datagridview.DataSource = table1;
  optimaldataGridView.DataSource = table2;
}

private void button1_Click(object sender, EventArgs e) {
  if (csv_datagridview.SelectedRows.Count > 0) {
    foreach (DataGridViewRow row in csv_datagridview.SelectedRows) {
      DataRowView dr = (DataRowView)row.DataBoundItem;
      table2.Rows.Add(dr.Row.ItemArray[0], dr.Row.ItemArray[1], dr.Row.ItemArray[2]);
    }
    optimaldataGridView.Refresh();
  }
}

private DataTable GetTable(string name) {
  DataTable table = new DataTable(name);
  table.Columns.Add("col1");
  table.Columns.Add("col2");
  table.Columns.Add("col3");
  return table;
}

private void FillTable(DataTable table) {
  for (int i = 0; i < 10; i++) {
    table.Rows.Add("R" + i + "C0", "R" + i + "C1", "R" + i + "C2");
  }
}

Hope this helps.

Upvotes: 1

Boosa
Boosa

Reputation: 19

Your code is working on my side.

  • Created a DataTable for Datasource of csv_datagridview.
  • Selected some rows in this grid.
  • Clicked the button to copy the selected rows to the optimaldataGridView

The selected rows are still selected.

public Form1()
{
   InitializeComponent();
   DataTable dt = new DataTable();
   dt.ReadXml(Application.StartupPath + @"\test.xml");
   csv_datagridview.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
   csv_datagridview.DataSource = dt;
}

private void button1_Click(object sender, EventArgs e)
{
   List<object> destList = new List<object>();
   foreach (DataGridViewRow row in csv_datagridview.SelectedRows)
      destList.Add(row.DataBoundItem);
   optimaldataGridView.DataSource = destList;
}

Make sure you have no events attached to the grids that may affect the selection.

Upvotes: 0

Related Questions