Reputation: 1636
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;
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?
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(...));
}
Upvotes: 3
Views: 5959
Reputation: 53595
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 SortableBindingList
s 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 SortableBindingList
s 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.
Upvotes: 3