简体   繁体   English

索引超出范围c#datagridview

[英]Index out of range c# datagridview

I am trying to display the result of an SQL query into a datagridview as follows: 我试图将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();
    }

However when I run the code, I get an error where the first row's column is to be inserted saying 但是,当我运行代码时,出现错误,其中第一行的列要插入

Index was out of range. 索引超出范围。 Must be non-negative and less than the size of the collection. 必须为非负数并且小于集合的大小。 Parameter name: index 参数名称:索引

What did I do wrong ? 我做错了什么 ?

You populate the datagridview by reading a DataReader . 您可以通过读取DataReader填充datagridview In this case, you need to explicitly add the rows to the datagridview . 在这种情况下,您需要将行显式添加到datagridview You currently try to access a row that doesn't exist yet. 您目前正在尝试访问尚不存在的行。

Suppose datagridview is empty when you start the loop. 假设启动循环时datagridview为空。 Just add: 只需添加:

while (reader_.Read())
            {
            dbgCPNlist.Rows.Add();
            ...

Crhis Answered Why this error, but why you doing in this way? Crhis回答为什么会出现此错误,但为什么要这样做呢?

You can get DataTable by using SqlDataAdapter and bind it directly to the GridView as below 您可以使用SqlDataAdapter获得DataTable并将其直接绑定到GridView,如下所示

using(var conn = new SqlConnection(connString))
{
    conn.Open();
    var command = new SqlCommand(sqlstring, conn);
    var adapter = new SqlDataAdapter(command);
    DataTable dt = new DataTable();
    adapter.Fill(dt);
    dbgCPNlist.DataSource = dt;
    dbgCPNlist.DataBind();
}
            while (reader_.Read())
            {
                dbgCPNlist.Rows.Add();
                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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM