try this
it will handle for any number of cols
int matchedCount = 0;
int unMatchedCount = 0;
int dgv1RowsCount = dataGridView1.Rows.Count;
int dgv2RowsCount = dataGridView2.Rows.Count;
int dgv1ColsCount = dataGridView1.Columns.Count;
int dgv2ColsCount = dataGridView2.Columns.Count;
int minRows = dgv1RowsCount <= dgv2RowsCount ? dgv1RowsCount : dgv2RowsCount;
int minCols = dgv1ColsCount <= dgv2ColsCount ? dgv1ColsCount : dgv2ColsCount;
int maxRows = dgv1RowsCount >= dgv2RowsCount ? dgv1RowsCount : dgv2RowsCount;
int maxCols = dgv1ColsCount >= dgv2ColsCount ? dgv1ColsCount : dgv2ColsCount;
for (int i = 0; i < minRows; i++)
for (int j = 0; j < minCols; j++)
if (dataGridView1.Rows[i].Cells[j].Value == dataGridView2.Rows[i].Cells[j].Value)
matchedCount++;
else
unMatchedCount++;
int extraRows = (dgv1RowsCount != dgv2RowsCount) ? Math.Abs(dgv1RowsCount - dgv2RowsCount) : 0;
int extraCols = (dgv1ColsCount != dgv2ColsCount) ? Math.Abs(dgv1ColsCount - dgv2ColsCount) : 0;
int extraUnMatched = extraRows * maxCols;
extraUnMatched += extraCols * maxRows;
unMatchedCount += extraUnMatched;
MessageBox.Show("Matched: " + matchedCount+ "\r\n" + "Not Matched: " + unMatchedCount);