我有一个DataGridView
,需要在按下按钮时从列表中更新。该列表填充了数据库提取的结果。
然而,当我执行时,
DataGridView.Update(list);
什么都没发生。
还有,我找到了这些“解决方案”:
BindingList
:BindingList<Meal> mVals=new BindingList<Meal>();
DataSource
重置为null
,然后重新更新:orderGridView.DataSource = null;
orderGridView.DataSource = mVals;
然而,这些都不适合我。我的DataGridView
没有像我预期的那样得到更新。
这是使用的代码:
String mName=Convert.ToString(itemPanel.SelectedItem);
Meal ml = new Meal();
BindingList<Meal> mVals=new BindingList<Meal>();
using (SqlConnection myConnection = new SqlConnection("server=CHAYU\\SQLEXPRESS;" +
"Trusted_Connection=yes;" +
"database=restaurantApp; " +
"connection timeout=30"))
{
string oString = "Select * from Meal where mealName='mName'";
SqlCommand oCmd = new SqlCommand(oString, myConnection);
myConnection.Open();
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{
ml.mealID = Convert.ToInt32(oReader["mealId"]);
ml.mealName = oReader["mealName"].ToString();
ml.price = Convert.ToInt32(oReader["price"]);
ml.timeToProduce = Convert.ToInt32(oReader["timeToProduce"]);
ml.availability = true;
ml.quantity = Convert.ToInt32(quant.Text);
mVals.Add(ml);
}
myConnection.Close();
}
}
orderGridView.DataSource = null;
orderGridView.DataSource = mVals;
答案 0 :(得分:0)
mVals
似乎为空,您应该将mList.Add(ml)
替换为mVals.Add(ml)
修改强>
另请查看以下内容:
string oString = "Select * from Meal where mealName='mName'";
mName不是变量。
orderGridView.AutoGenerateColumns是真的吗?