我在一个用户控件中有一个按钮单击事件,只要它与字符串数组中找到的成分匹配,就会从tbl_ingredients的成分库存中扣除1。
private void btn_confirm_Click(object sender, EventArgs e, string[] ings)
{
foreach (string s in ings)
{
string qry = "UPDATE tbl_ingredients SET inv_stock = inv_stock -1 WHERE inv_name = '" + s + "'";
SQLiteCommand myCommand = new SQLiteCommand(qry, myConnection);
myCommand.ExecuteNonQuery();
}
}
和另一个用户控件中动态创建的tablelayoutpanel(以显示所有成分及其各自的总库存)
myConnection = new SQLiteConnection("Data Source=database.sqlite3");
string qry = "SELECT * FROM tbl_ingredients ORDER BY inv_name";
string qry2 = "SELECT COUNT(*) FROM tbl_ingredients";
SQLiteCommand myCommand = new SQLiteCommand(qry, myConnection);
SQLiteCommand myCommand2 = new SQLiteCommand(qry2, myConnection);
openConnection();
int row = Convert.ToInt32(myCommand2.ExecuteScalar());
SQLiteDataReader result = myCommand.ExecuteReader();
string[] itemname = new string[row];
string[] totalstock = new string[row];
int cnt = 0;
if (result.HasRows)
{
while (result.Read())
{
itemname[cnt] = result["inv_name"].ToString();
totalstock[cnt] = result["inv_stock"].ToString();
cnt++;
}
}
//tlb_inventory is the name of the tablelayoutpanel in windows form
tbl_inventory.ColumnCount = 2;
tbl_inventory.RowCount = row;
tbl_inventory.Controls.Add(new Label() { Text = "Item" }, 0, 0);
tbl_inventory.Controls.Add(new Label() { Text = "Total Stock" }, 1, 0);
for (int i = 1, j = 0; i < row + 1; i++, j++)
{
tbl_inventory.Controls.Add(new Label() { Text = itemname[j], AutoSize = true }, 0, i);
tbl_inventory.Controls.Add(new Label() { Text = totalstock[j], AutoSize = true }, 1, i);
}
closeConnection();
每当我单击按钮时,它都应该实时更新表的内容。问题是我必须重新运行该程序才能显示表的更新内容。单击按钮后,是否有功能或某些功能可以使用户控件及其内容刷新?
答案 0 :(得分:2)
首先,您需要将name属性设置为标签控件
tbl_inventory.Controls.Add(new Label() { Name = "Total_"+ itemname[j], Text = totalstock[j], AutoSize = true }, 1, i);
,然后在foreach循环内的button_click事件中添加:
Label c = (tbl_inventory.Controls.Find("Total_" + s, true).First() as Label);
var total = Convert.ToInt32(c.Text);
c.Text = (total++).ToString();