2

次のように、SQLクエリの結果をに表示しようとしてdatagridviewいます:

SqlCommand cmd;
SqlDataReader reader_;
SqlConnection ccs = new SqlConnection(MainForm._constR.ToString());

string strSQL_ = "SELECT cp.CouponNumber as cpn, cp.StopOverCode as xo, cp.ReservationBookingDesignator as RBD, cp.Carrier as carrier, cp.FlightNumber as flightNum, cp.UsedClassofService as class_of_service, cp.FlightDepartureDate as f_d_d, cp.FlightDepartureTime as f_d_t,  cp.CouponStatus as cp_stat, cp.FareBasisTicketDesignator as farebasis,  cp.NotValidBefore as val_bef,  cp.NotValidAfter as val_aft,  cp.FreeBaggageAllowance as free_bag_allow,  cp.FlightBookingStatus as f_booking_stat, cp.OriginAirportCityCode+'/'+cp.DestinationAirportCityCode as GFPA,cp.UsageOriginCode +'/'+ cp.UsageDestinationCode as usg_sector,  cp.UsageAirline as usg_airline,  cp.UsageDate as usg_date,  cp.UsageFlightNumber as usg_f_num,  cp.FrequentFlyerReference as FFP  FROM [Biatss_PC].[Pax].[SalesDocumentCoupon] as cp  JOIN [Biatss_PC].[Pax].[SalesDocumentHeader] as h on h.DocumentNumber = cp.DocumentNumber  WHERE h.DocumentNumber = '2581806273'  ";//document number to be parsed
//objCmd = new OleDbCommand(strSQL, objConnection);
cmd = ccs.CreateCommand();
ccs.Open();
cmd.CommandText = strSQL_;
reader_ = cmd.ExecuteReader();

int r_ = 0;
int c_ = 0;

if (reader_.HasRows)
    {

        while (reader_.Read())
        {

            dbgCPNlist.Rows[r_].Cells[c_].Value =  reader_["cpn"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["xo"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["GFPA"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["carrier"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["flightNum"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["class_of_service"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["f_d_d"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["f_d_t"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["cp_stat"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["farebasis"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["val_bef"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["val_aft"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["free_bag_allow"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["f_booking_stat"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["RBD"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["usg_sector"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["usg_airline"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["usg_f_num"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["usg_date"].ToString();
            c_++;

            dbgCPNlist.Rows[r_].Cells[c_].Value = reader_["FFP"].ToString();
            c_++;

            //so as to be able to start at column-index zero for next row
            c_ = 0;
            r_++;

        }
        ccs.Close();
    }

ただし、コードを実行すると、最初の行の列が挿入されるというエラーが表示されます

インデックスが範囲外でした。負ではなく、コレクションのサイズより小さくなければなりません。パラメータ名:インデックス

私は何を間違えましたか?

4

3 に答える 3

3

datagridviewを読み取って に入力しDataReaderます。この場合、行を明示的に に追加する必要がありますdatagridview。現在、まだ存在しない行にアクセスしようとしています。

ループdatagridviewを開始するときに が空であるとします。追加するだけです:

while (reader_.Read())
            {
            dbgCPNlist.Rows.Add();
            ...
于 2013-09-20T09:45:33.483 に答える