0

DataSet にクエリを実行し、バインドされていない DataGridView に結果を表示しようとしています。ここでプログラミングロジックにかなり近づいているように感じますが、エラーが発生し続けますArgumentOutOfRange Exception. Index was out of range. Must be non-negative and less than the size of the collection.

私のコードスニペット:

DataRow[] foundRows;

//Queries the Reservations table with the 'searchExpression' variable
foundRows = this.reservationMasterDataSet.Tables["Reservations"].Select(searchExpression);

//If there is at least one record found...
if (foundRows.Length > 0)
{
    //Used to count our row indexes
    int i = 0;

    //Populate the DataGridView with the queried response
    foreach (DataRow row in foundRows)
        {
            //Used to count our column indexes
            for (int j = 0; j < reservationMasterDataSet.Tables["Reservations"].Columns.Count; j++)
            {
                //THIS LINE IS THROWING AN EXCEPTION
                dataGridView1.Rows[i].Cells[j].Value = row.ItemArray[j];        
            }

            i++;
        }
}

MyDataRowには 12 個のオブジェクトが含まれているため、対応する列が 12 個あることを確認しましたDataGridView(元のデータベースには 12 個あります)。すぐに例外が発生していると思います(iまだ0デバッガーにあります)。最初に just を使用して試しましrow[i]たが、同じエラーが発生しました。

これは、編集可能なものではなく、検索結果ペインであることを意図しているため、特定の結果のみを返したいのです。DataGridViewこれが、Windows フォームにレコードをレイアウトするための最も適切で簡単な方法であると考えました。

4

1 に答える 1