Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在单击按钮时刷新用户控件中的tablelayoutpanel内容_C#_Winforms_Sqlite_Tablelayoutpanel - Fatal编程技术网

C# 如何在单击按钮时刷新用户控件中的tablelayoutpanel内容

C# 如何在单击按钮时刷新用户控件中的tablelayoutpanel内容,c#,winforms,sqlite,tablelayoutpanel,C#,Winforms,Sqlite,Tablelayoutpanel,我在一个用户控件中有一个按钮单击事件,每当它与字符串数组中的成分匹配时,它将从tbl_成分中的成分库存中扣除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_n

我在一个用户控件中有一个按钮单击事件,每当它与字符串数组中的成分匹配时,它将从tbl_成分中的成分库存中扣除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();
myConnection=newsqliteconnection(“数据源=database.sqlite3”);
string qry=“按库存名称从tbl\U配料订单中选择*”;
字符串qry2=“从tbl_配料中选择计数(*)”;
SQLiteCommand myCommand=新的SQLiteCommand(qry,myConnection);
SQLiteCommand myCommand2=新的SQLiteCommand(qry2,myConnection);
openConnection();
int row=Convert.ToInt32(myCommand2.ExecuteScalar());
SQLiteDataReader结果=myCommand.ExecuteReader();
string[]itemname=新字符串[行];
字符串[]totalstock=新字符串[行];
int-cnt=0;
if(result.HasRows)
{
while(result.Read())
{
itemname[cnt]=结果[“inv_name”]。ToString();
totalstock[cnt]=结果[“库存”]。ToString();
cnt++;
}
}
//tlb_inventory是windows窗体中tablelayoutpanel的名称
tbl_inventory.ColumnCount=2;
tbl_inventory.RowCount=行;
tbl_inventory.Controls.Add(new Label(){Text=“Item”},0,0);
tbl_inventory.Controls.Add(new Label(){Text=“Total Stock”},1,0);
对于(inti=1,j=0;i

每当我点击按钮时,它都会实时更新表的内容。问题是我必须重新运行程序,以便它显示表的更新内容。是否有一个函数或某些东西可以使用户控件及其内容在单击按钮后刷新?

首先需要将name属性设置为labels控件

 tbl_inventory.Controls.Add(new Label() { Name = "Total_"+ itemname[j], Text = totalstock[j], AutoSize = true }, 1, i);
然后在按钮上单击foreach循环内的事件添加:

 Label c = (tbl_inventory.Controls.Find("Total_" + s, true).First() as Label);
 var total = Convert.ToInt32(c.Text);
 c.Text = (total++).ToString();

首先,需要将name属性设置为labels控件

 tbl_inventory.Controls.Add(new Label() { Name = "Total_"+ itemname[j], Text = totalstock[j], AutoSize = true }, 1, i);
然后在按钮上单击foreach循环内的事件添加:

 Label c = (tbl_inventory.Controls.Find("Total_" + s, true).First() as Label);
 var total = Convert.ToInt32(c.Text);
 c.Text = (total++).ToString();

在刷新表的
btn\u confirm\u Click
方法上没有调用任何方法。在哪里调用第二个代码块?在刷新表的
btn\u confirm\u Click
方法上没有调用任何方法。在哪里调用第二个代码块?