DataGridView合并

时间:2011-08-19 20:28:52

标签: c# datagridview merge

是否可以将C#中的2个单独DataGridView合并为一个?我为每个SortableBindingList使用两个单独的DataGridView

以下是名为 theChipDGV的第一个DataGridView的代码。

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

以下是名为 theDataBaseDGV的第二个DataGridView的代码。

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));

theFinalDGV.DataSource = theChipList + theDataBaseList;

其他一些方式,因为我认为这两种方法都不起作用?


如果是这样,这怎么可能?

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));

然而,使用它只能抓住一行,我需要抓住每一行..即使一个列表比另一个列表大,我想填补空白点..

我试着弄乱这个:

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(...));
}

1 个答案:

答案 0 :(得分:3)

这里有几种方法。

我不熟悉SortableBindingList,但您可以将两个列表合并为一个,然后将其绑定到DataGridView。如果两个SortableBindingList拥有不同类型(一个是Chip,另一个DataBase)的问题是一个问题,您可以始终创建一个接口并让两个类型实现它。然后,您可以将SortableBindingList中的类型T更改为您的界面,然后将该组合列表组合并绑定到DataGridView。

另一种方法是将DataGridViews中的行复制到第三个DataGridView中。我拿了示例here并创建了一个方法,该方法将获取DataGridView并返回其DataGridViewRows的数组。

以下是方法:

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;
}

然后,您可以使用此代码将行复制到两个DGV(dataGridView1和dataGridView2)中的第三个DGV(dataGridView3)。

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

请注意,此方法将复制添加到DGV的“空”行,即DGV的AllowUserToAddRows为真。如果是这种情况并且您不想要那个空行,请将rowArray的大小调整为比我显示的小一个,并在外部for循环中测试一个新行,如下所示:

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

此外,在合并之前,请确保目标DGV与源DGV具有相同的列数。

相关问题