3

Is it possible to merge 2 seperate DataGridView in C# into one? I am using a two seperate SortableBindingList for each DataGridView.

Here is the code for the first DataGridView called theChipDGV.

theChipList.Add(new Chip(splitLine[0], splitLine[1], xValue, yValue, rotation, splitLine[5]));
theChipDGV.DataSource = theChipList;

Here is the code for the second DataGridView called theDataBaseDGV.

theDataBaseList.Add(new DataBase(dataBaseSplit[0], dataBaseSplit[1], dataBaseSplit[2], dataBaseSplit[3], dataBaseSplit[4], dataBaseSplit[5], dataBaseSplit[6], 0));
theDataBaseDGV.DataSource = theDataBaseList;

EDIT:

theFinalList.Add(new Final(splitLine[0], splitLine[1], xValue, yValue, rotation, splitLine[5], dataBaseSplit[0], dataBaseSplit[1],
                           dataBaseSplit[2], dataBaseSplit[3], dataBaseSplit[4],dataBaseSplit[5], dataBaseSplit[6], 0));

OR

theFinalDGV.DataSource = theChipList + theDataBaseList;

OR

some other way since both of these I do not think will work?


If so, how is this possible?

EDIT2:

theLoadList.Add(new LoadLine(theChipList[0].Name, theChipList[0].PartNumber,
                theChipList[0].XPlacement, theChipList[0].YPlacement, theChipList[0].Rotation,
                theChipList[0].PkgStyle, theDataBaseList[0].PackageType, theDataBaseList[0].PartDescription,
                theDataBaseList[0].Feeder, theDataBaseList[0].Vision, theDataBaseList[0].Speed,
                theDataBaseList[0].Machine, theDataBaseList[0].TapeWidth, 0));

However using this only grabs one row and I need to grab every row.. Even if one list is larger than the other I would like to fill in the blank spots..

I tried messing with this:

int chipRowCount = theChipDGV.Rows.Count;
int dataBaseRowCount = theDataBaseDGV.Rows.Count;
int greaterValue = 0;

if (chipRowCount > dataBaseRowCount)
    greaterValue = chipRowCount;
else
    greaterValue = dataBaseRowCount;

int i = 1;

while (i < greaterValue)
{
    // Place the above **EDIT2** code here. (theLoadList.Add(new LoadLine(...));
}
theNoobGuy
  • 1,636
  • 6
  • 29
  • 45
  • do you mean at the visual level or the data level? What result do you want from your merge operation? – Aaron Anodide Aug 19 '11 at 21:20
  • @Gabriel: Well since it is a using a `SortableBindingList` it would do both the data and the visual levels... The answer has been updated to show what I want from the merge operation... – theNoobGuy Aug 19 '11 at 21:49

1 Answers1

3

There are a couple of approaches here.

I'm not familiar with the SortableBindingList but you might be able combine your two lists into one and then bind that to a DataGridView. If the fact that the two SortableBindingLists hold different types (one is Chip, the other DataBase) is a problem you can always create an Interface and have both your types implement it. You can then change the type T in your SortableBindingLists to your interface and then combine and bind this combined list to your DataGridView.

The other approach is to copy the rows in both your DataGridViews into a third DataGridView. I took the example here and created a method which will take a DataGridView and return an array of its DataGridViewRows.

Here's the method:

private DataGridViewRow[] CloneDataGridViewRows(DataGridView dgv) {
    var rowArray = new DataGridViewRow[dgv.Rows.Count];
    for (int i = 0; i < dgv.Rows.Count; i++) {
        DataGridViewRow clonedRow = (DataGridViewRow)dgv.Rows[i].Clone();
        for (int c = 0; c < clonedRow.Cells.Count; c++) {
            clonedRow.Cells[c].Value = dgv.Rows[i].Cells[c].Value;
        }
        rowArray[i] = clonedRow;
    }
    return rowArray;
}

You can then use this code to copy the rows into your two DGVs (dataGridView1 and dataGridView2) into a third DGV (dataGridView3).

dataGridView3.Rows.AddRange(CloneDataGridViewRows(dataGridView1));
dataGridView3.Rows.AddRange(CloneDataGridViewRows(dataGridView2));

Note that this method will copy the 'empty' row added to the DGV is the DGV's AllowUserToAddRows is true. If this is the case and you don't want that empty row, size your rowArray to one less than what I show, and test for a new row in the outer for loop for such a row like this:

if (!dgv.Rows[i].IsNewRow) {
    // Do the copy...
}

Also, before you merge make sure your destination DGV has the same number of columns as your source DGVs.

Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • What if instead of copying the rows, I wanted to copy the columns from the **second `DataGridView`** at the end of the **first `DataGridView`**. So if the first DGV had 4 columns and the second DGV had 2 columns, it would go: `DGV1-column1 DGV1-column2 DGV1-column3 DGV1-column4 DGV2-column1 DGV2-column2`.. as well as every row in the columns being copied too – theNoobGuy Aug 19 '11 at 21:56
  • @theNoobGuy You could add a column to your first DGV and then loop through the rows in your second DGV. For each row copy the value of the cell from your first DGV to the cell (new from the column add) in your second DGV. None the other values in the row in your second DGV would be modified if you did this. Obviously this depends on the data in the corresponding rows btween the two DGV being 'synced.' (ie, they make sense when they're combined like this.) – Jay Riggs Aug 19 '11 at 22:20
  • Error stating: No row can be added to a DataGridView control that does not have columns. Columns must be added first. – theNoobGuy Aug 19 '11 at 22:20
  • I should have added that that third DGV needs to have the correct number of columns before you merge! – Jay Riggs Aug 19 '11 at 22:21
  • I have been messing around with your code and seem to get it closer to what I am looking for.. However not exact. I updated my question about with more information if you care to take a look :). Thanks – theNoobGuy Aug 20 '11 at 22:38