と私は2 Datagridviewsを有する、C#のWindowsFormsを使用して複数の列
をDataGridViewの行のデータを比較すると、それぞれのDataGridViewは2列を有しています。
DT1とDT2行を比較したいのですが、DT1行(Col1とCol2)にDT1(Col1とCol2)が見つかったらDT1行とDT2行が削除/削除されます。
Diagram of the explained above
Error Row index provided is out of range
Code Start Here
{
bool MatchFound = false;
List<int> IndexToRemoveFromGrid = new List<int>();
//Loop Table 1 and get Row Values for Col1+Col2
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewRow T1Col1row = dataGridView1.Rows[row.Index];
DataGridViewRow T1Col2row = dataGridView1.Rows[row.Index];
string T1Col1RX = (string)T1Col1row.Cells[0].Value + (string)T1Col1row.Cells[1].Value;
MatchFound = false;
//Loop Table 2 and get Row Values for Col1+Col2
foreach (DataGridViewRow rowT2 in dataGridView2.Rows)
{
DataGridViewRow T2Col1row = dataGridView2.Rows[rowT2.Index];
DataGridViewRow T2Col2row = dataGridView2.Rows[rowT2.Index];
string T2Col1RX = (string)T2Col1row.Cells[0].Value + (string)T2Col1row.Cells[1].Value;
if (MatchFound == false)
{
//Compare Table 1 RX to Table2 RX , If matched then deleted row on both sides
if (String.Compare(T1Col1RX, T2Col1RX, false) == 0)
{
if (row.Index > -1 && rowT2.Index > -1)
{
IndexToRemoveFromGrid.Add(row.Index);
}
MatchFound = true;
}
}
}
}
for (int i = 0; i <= IndexToRemoveFromGrid.Count(); i++)
{
dataGridView1.Rows.RemoveAt(i);
dataGridView2.Rows.RemoveAt(i);
}
}
Datagridviewsにはデータのコレクションがあり、内容を比較して不要なものをコンテナから削除するだけです。 – Gnqz
私はこれを書きましたが、私はインデックス値の問題に直面しています。返り値-1と例外 – Yazzan
あなたはどのような例外を取得しましたか? – Gnqz