【发布时间】:2014-12-16 06:32:41
【问题描述】:
我有一个 win 表单,它有一个 datagridview 和一个按钮。当我按下该按钮时,它会从Order 类调用viewOrderHistory() 方法。从那里我想向datagridview添加行。我的表单名称 orderHistory。这是我的viewOrderHistory 方法。
OrderHistory OH = new OrderHistory();
public void viewOrderHistory(int id){
DataGridView orderHistoryProductDataGrid = OH.dataGridView2;
try
{
int count = 0;
orderHistoryProductDataGrid.Rows.Clear();
orderHistoryProductDataGrid.Refresh();
DatabaseConnection();//Database connection
string ord = "SELECT * FROM Orders_Products WHERE ID='" + id + "'";
SqlCommand ordPro = new SqlCommand(ord, myCon);
SqlDataReader rdr = ordPro.ExecuteReader();
while (rdr.Read())
{
DataGridViewRow row = (DataGridViewRow)orderHistoryProductDataGrid.Rows[count].Clone();
row.Cells[0].Value = rdr["Code"].ToString();
row.Cells[1].Value = mainform.getProduct(rdr["Code"].ToString());
row.Cells[2].Value = rdr["Quantity"].ToString(); ;
orderHistoryProductDataGrid.Rows.Add(row);
count++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}`
从数据库中成功检索数据。但是行没有添加到我的数据网格视图中。请帮忙...
【问题讨论】:
-
检查我的答案,如果有不清楚的地方告诉我!
标签: c# datagridview