我需要根据组合框中选择的PO_No将所选数据显示到我的DataGridView中。我试过这个。但是只显示了选定PO_No的一行
public void loadPOCarttable()
{
DynamicConnection con = new DynamicConnection();
try
{
con.sqlquery("select b.BookName,b.ISBN_No,p.OrderQuantity,p.UnitPrice,p.Total from TBL_Book b, TBL_PO_Cart p where b.ISBN_No=p.ISBN_No and PO_No='" + cmbPO.Text + "'");
con.mysqlconnection();
con.datatable();
con.dataread();
//con.table.Load(con.datareader);
int i = 0;
while (con.datareader.Read())
{
dataGridView1.Rows[i].Cells[0].Value = con.datareader["BookName"].ToString();
dataGridView1.Rows[i].Cells[1].Value = con.datareader["ISBN_No"].ToString();
dataGridView1.Rows[i].Cells[2].Value = con.datareader["OrderQuantity"].ToString();
dataGridView1.Rows[i].Cells[3].Value = con.datareader["UnitPrice"].ToString();
dataGridView1.Rows[i].Cells[4].Value = con.datareader["Total"].ToString();
i++;
}
//this.dataGridView1.DataSource = con.table;
}
catch (Exception ex)
{
MessageBox.Show("Error Occured" + ex.Message);
}
}
并显示此错误。如何解决此问题 enter image description here
答案 0 :(得分:0)
首先确保您需要所有细胞,然后尝试这样:
while (con.datareader.Read())
{
DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
row.Cells[0].Value = con.datareader["BookName"].ToString();
row.Cells[1].Value = con.datareader["ISBN_No"].ToString();
row.Cells[2].Value = con.datareader["OrderQuantity"].ToString();
row.Cells[3].Value = con.datareader["UnitPrice"].ToString();
row.Cells[4].Value = con.datareader["Total"].ToString();
dataGridView1.Rows.Add(row);
}
<强>更新强>
使用this.dataGridView1.Rows.Clear();
清除以前的数据。