简体   繁体   中英

Unhandled Exception when Filling a datagrid View

I'm working on a C# winform application. My goal is after the user picked a date from the datetimePicker, a query is performed and returns results with only identical records. Next, the user will select one of these records which should fill the grid with all items ordered associated with that customer from a combobox (custlocation) This part is working. Problem is, now I've been stuck trying to get my dataGridView1 to populate. I keep getting:

an unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

I've checked my query and it returns the correct results.

private void custlocation_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlDataAdapter da = new SqlDataAdapter(
        "Select [orderitem].*, [orderitem].qty, [orderitem].descrip, [orderitem].tally "+
        "from [orderitem] inner join [Order] on [orderitem].orderId = [order].ct "+
        "where [order].loadDate='" + dateTimePicker1.Value.Date + "' "+
        "and [order].addr1=" + custlocation.Text, conn);

    DataTable lt = new DataTable();
    //da.Fill(lt);

    //dataGridView1.DataSource = lt;
    dataGridView1.AutoGenerateColumns = true;
    dataGridView1.DataSource = lt;
    dataGridView1.Columns[4].Name = "loadtickets";
    dataGridView1.Rows[a].Cells[0].Value = rdr["orderID"].ToString();
    dataGridView1.Rows[a].Cells[1].Value = rdr["qty"].ToString();
    dataGridView1.Rows[a].Cells[2].Value = rdr["descrip"].ToString();
    dataGridView1.Rows[a].Cells[3].Value = rdr["tally"].ToString();
}

Arrays are zero-based, like you used on the Cells property. Seems you are trying to change the name of the 5th column, but you only have 4.

Change

dataGridView1.Columns[4].Name = "loadtickets";

To

dataGridView1.Columns[3].Name = "loadtickets";

an unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

This error is because you have to use dataGridView1.Columns[3].Name = "loadtickets"; like @Anderson Pimentel said.

The multi-part identifier "System.Data.DataRowView" could not be bound

This error is because of the query.

 SqlDataAdapter da = new SqlDataAdapter(
    "Select [orderitem].*, [orderitem].qty, [orderitem].descrip, [orderitem].tally "+
    "from [orderitem]  inner join [Order] on [orderitem].orderId = [order].ct "+
    "where [order].loadDate='" + dateTimePicker1.Value.Date + "' "+
    "and [order].addr1=" + custlocation.Text, conn);

you have selected orderitm.* (all columns from orderitem ) and 3 columns from the same table ( [orderitem].qty, [orderitem].descrip, [orderitem].tally ). so these columns are duplicated

You have to remove these duplicates or give them aliases like the [orderitem].qty AS Quantity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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