0

2 つのグリッドビューがあり、列ごとに結果を比較する必要があります。グリッドビューの 1 つに、他のグリッドビューにはない列がある場合があるため、両方のグリッドに存在する列のみを比較する必要があります。

私が書いたコードは、実際には各行のすべてのセルを通過し、行0セル0から始まり、行0セル1を続け、次の行に進みます。ただし、たとえばグリッド 2 の列がグリッド 1 に存在する場合、そのセルを通過してから次の列に渡すようにセルを作成したいと思います。以下は私が持っているコードです:

List<String> columnsGrid43 = new List<String>();

foreach (TableCell cell in gridReport43.Rows[0].Cells)
{
    BoundField fieldGrid43 = (BoundField)((DataControlFieldCell)cell).ContainingField;
    columnsGrid43.Add(fieldGrid43.DataField);
}

foreach (TableCell cell in gridReport44.Rows[0].Cells)
{
    BoundField fieldReportGrid44 = (BoundField)((DataControlFieldCell)cell).ContainingField;
    if (columnsGrid43.Contains(fieldReportGrid44.DataField))
    {
        for (int countRow = 0; countRow < gridReport44.Rows.Count; countRow++)
        {
            for (int countCell = 0; countCell < gridReport44.Rows[countRow].Cells.Count; countCell++)
            {
                string grid1Value = gridReport43.Rows[countRow].Cells[countCell].Text;
                string grid2Value = gridReport44.Rows[countRow].Cells[countCell].Text;
                if (grid2Value.Contains(grid1Value))
                {
                    gridReport43.Rows[countRow].Cells[countCell].BackColor = Color.FromArgb(101, 226, 75);
                    gridReport44.Rows[countRow].Cells[countCell].BackColor = Color.FromArgb(101, 226, 75);
                }
                else
                {
                    gridReport43.Rows[countRow].Cells[countCell].BackColor = Color.FromArgb(255, 102, 102);
                    gridReport44.Rows[countRow].Cells[countCell].BackColor = Color.FromArgb(255, 102, 102);
                }
            }
        }
    }
}
4

1 に答える 1

1

2 つの GridView の 2 つの DataTable を比較することをお勧めします。非常に簡単です。

次のようなさまざまな方法を使用できますDataTable.RowsContains, Find or CopyTo

リンク: http://msdn.microsoft.com/fr-fr/library/system.data.datarowcollection.aspx

ここでコードのサンプル:

    public static void CompareRows(DataTable table1, DataTable table2)
    {
    foreach (DataRow row1 in table1.Rows)
    {
        foreach (DataRow row2 in table2.Rows)
        {
        var array1 = row1.ItemArray;
        var array2 = row2.ItemArray;

        if (array1.SequenceEqual(array2))
        {
            Console.WriteLine("Equal: {0} {1}", row1["ColumnName"], row2["ColumnName"]);
        }
        else
        {
            Console.WriteLine("Not equal: {0} {1}", row1["ColumnName"], row2["ColumnName"]);
        }
        }
    }
    }
于 2013-03-28T13:13:11.880 に答える