在我看来,我的foreach循环没有到达我的第二个面板,或者某些东西让它跳过循环中的项目。
这是创建面板和循环(我知道SQL很容易被注入,但为了让问题更短,我只使用了SQL):
这是为产品表中的每个产品创建的内容:
Label lbName = new Label();
lbName.Text = name + "\n" + row[7].ToString();
lbName.Height = 40;
lbName.Width = 150;
lbName.Name = name + row[7].ToString();
lbName.Location = new Point(ptX, ptY);
pt.Controls.Add(lbName);
Label lbID = new Label();
lbID.Text = row[0].ToString();
lbID.Height = 40;
lbID.Width = 150;
lbID.Name = "ID" + name + row[7].ToString();
lbID.Location = new Point(ptX, ptY);
lbID.Visible = false;
pt.Controls.Add(lbID);
TextBox txtStockCount = new TextBox();
txtStockCount.Text = "0";
txtStockCount.Height = 40;
txtStockCount.Width = 100;
txtStockCount.Name = "txtTS" + name + row[7].ToString();
txtStockCount.Location = new Point(ptX + 150, ptY);
txtStockCount.GotFocus += txtStockCount_GotFocus;
txtStockCount.KeyPress += txtStockCount_KeyPress;
txtStockCount.LostFocus += txtStockCount_LostFocus;
pt.Controls.Add(txtStockCount);
TextBox txtBroken = new TextBox();
txtBroken.Text = "0";
txtBroken.Height = 40;
txtBroken.Width = 100;
txtBroken.Name = "txtTB" + name + row[7].ToString();
txtBroken.Location = new Point(ptX + 300, ptY);
txtBroken.GotFocus += txtStockCount_GotFocus;
txtBroken.KeyPress += txtStockCount_KeyPress;
txtBroken.LostFocus += txtStockCount_LostFocus;
pt.Controls.Add(txtBroken);
TextBox txtRecieve = new TextBox();
txtRecieve.Text = "0";
txtRecieve.Height = 40;
txtRecieve.Width = 100;
txtRecieve.Name = "txtTR" + name + row[7].ToString();
txtRecieve.Location = new Point(ptX + 450, ptY);
txtRecieve.GotFocus += txtStockCount_GotFocus;
txtRecieve.KeyPress += txtStockCount_KeyPress;
txtRecieve.LostFocus += txtStockCount_LostFocus;
pt.Controls.Add(txtRecieve);
Label lbDifference = new Label();
lbDifference.Text = " 0 ";
lbDifference.Height = 40;
lbDifference.Width = 150;
lbDifference.Name = "lblDiff" + name + row[7].ToString();
lbDifference.Location = new Point(ptX + 600, ptY);
pt.Controls.Add(lbDifference);
Button btnConfirm = new Button();
btnConfirm.Text = "Confirm";
btnConfirm.Height = 30;
btnConfirm.Width = 80;
btnConfirm.Name = "btnConfirm" + name + row[7].ToString();
btnConfirm.Location = new Point(ptX + 750, ptY);
btnConfirm.Click += btnConfirm_Click;
pt.Controls.Add(btnConfirm);
并从文本框中读取面板以更新数据库:
private void txtStockCount_LostFocus(object sender, EventArgs e)
{
TextBox txtB = (sender as TextBox);
string name = txtB.Name.Remove(0,5);
string prodID = "";
string QtyToday = "";
string QtyBD = "";
string QtyRev = "";
string table = "";
foreach (Panel p in pnls)
{
IEnumerable<Label> labels = p.Controls.OfType<Label>();
foreach (Label label in labels)
{
string Compare = label.Name.Remove(0, 2);
if (name == Compare)
{
prodID = label.Text;
if (Regex.IsMatch(Compare, @"^[a-hA-H]"))
{
table = "ProductHistoryAH";
}
else if (Regex.IsMatch(Compare, @"^[i-qI-Q]"))
{
table = "ProductHistoryIQ";
}
else if (Regex.IsMatch(Compare, @"^[r-zR-Z]"))
{
table = "ProductHistoryRZ";
}
}
}
string sql = "Select * From Product WHERE ProdID = '" + prodID + "'";
DataTable dtP = db.GetDataTable(sql);
IEnumerable<TextBox> textBoxes = p.Controls.OfType<TextBox>();
foreach (TextBox textBox in textBoxes)
{
string name1 = textBox.Name.Remove(0, 5);
label2.Text = name1;
if (name == name1)
{
if (textBox.Name == "txtTS" + dtP.Rows[0][1].ToString() + dtP.Rows[0][7].ToString())
{
QtyToday = textBox.Text;
}
if (textBox.Name == "txtTB" + dtP.Rows[0][1].ToString() + dtP.Rows[0][7].ToString())
{
QtyBD = textBox.Text;
}
if (textBox.Name == "txtTR" + dtP.Rows[0][1].ToString() + dtP.Rows[0][7].ToString())
{
QtyRev = textBox.Text;
}
}
}
string sql1 = "Select * From " + table + " WHERE ProdID = '" + prodID + "' AND ProdHDate = '" + DateTime.Today.Date.ToShortDateString() + "'";
DataTable dt = db.GetDataTable(sql1);
if (dt.Rows.Count == 0)
{
dbh.addnewstockhistory(ph);
}
else if (dt.Rows.Count > 0)
{
dbh.updatestockhistory(ph);
}
}
}
一切工作100%,它可以轻松插入和更新数据,第一个面板但其他面板上没有其他文本框可用。
错误在于foreach面板循环,foreach循环在面板循环内。它说字符串sql1附近有语法错误。但我不明白的是,它适用于第一个面板,但不适用于其他面板,它确实有prodID,我知道这个原因我将声明放在标签中只是为了看它的外观。
请帮忙。如果你需要,可以要求任何东西。
答案 0 :(得分:0)
我想我解决了。我所要做的就是检查面板是否是托管控件的面板,检查面板是否是父按钮并且它是否有效。
if(p == btn.Parent)
{
//Do my stuff
}
在我的foreach面板循环中。