我正在继续上一个问题,因为我从来没有得到完整的答复。 Reference previous object created in loop to update SQL
基本上,我已将语法更改为以下内容,但有两个问题:
我无法使用Controls.Add(myCheckBox);当你进行更新时,它会根据循环中创建的最后一个复选框更新SQL中的所有内容 - 它不会计算每个复选框。
我还在学习C#。非常感谢你的进步。
更新代码:
public partial class SelectUsers : Form {
// DECLARE THE myCheckBox
private CheckBox myCheckBox;
public SelectUsers()
{
InitializeComponent();
}
private void SelectUsers_Load(object sender, EventArgs e)
{
DataSet ds = myconnection.runSelect(new DataSet(), "THE SELECT");
int xAxisCheckbox = 40;
int yAxisCheckbox = 50;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
// CHANGED List<CheckBox> myBoxes LINE
List<CheckBox> myBoxes = new List<CheckBox>();
myCheckBox = new CheckBox();
myCheckBox.Location = new Point(xAxisCheckbox, yAxisCheckbox);
myCheckBox.Size = new Size(120, 20);
myCheckBox.Text = ds.Tables[0].Rows[i]["FullName"].ToString();
myCheckBox.Checked = (bool)ds.Tables[0].Rows[i]["InOperation"];
yAxisCheckbox = yAxisCheckbox + 80;
// THIS SHOULD ADD/RENDER EACH CHECKBOX, BUT IT DOESN'T
myBoxes.Add(myCheckBox);
}
}
private void saveBtn_Click(object sender, EventArgs e)
{
DataSet ds = myconnection.runSelect(new DataSet(), "THE SELECT");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
userID = (int)ds.Tables[0].Rows[i]["UserID"];
if (myBoxes[i].Checked)
{
myconnection.runUpdate("UPDATE Users SET InOperation = 1, OperationOrder = CASE WHEN OperationOrder = 1 THEN 1 ELSE CASE WHEN InOperation=1 THEN OperationOrder ELSE (SELECT COUNT(*)+1 FROM Users WHERE InOperation=1 AND OperationOrder > 0) END END WHERE UserID=" + userID);
myconnection.runUpdate("UPDATE Users SET OperationOrder = CASE WHEN OperationOrder = 0 THEN (SELECT COUNT(*) FROM Users WHERE InOperation=1) ELSE OperationOrder END WHERE InOperation=1");
}
else
{
myconnection.runUpdate("UPDATE Users SET InOperation = 0, OperationOrder = 0 WHERE UserID=" + userID);
myconnection.runUpdate("UPDATE Users SET OperationOrder = CASE WHEN OperationOrder -1 = 0 THEN (SELECT COUNT(*) FROM Users WHERE InOperation=1) ELSE OperationOrder -1 END WHERE InOperation=1");
}
}
}
}
答案 0 :(得分:0)
阅读编程书籍后,我发现你没有像这样创建一个数组,它是通过以下方式完成的:
// Global
CheckBox[] myCheckBox;
DataSet ds;
private void SelectUsers_Load(object sender, EventArgs e) {
ds = myconnection.runSelect(new DataSet(), "THE SELECT");
int xAxisCheckbox = 40;
int yAxisCheckbox = 50;
// Create the CheckBox array (amount based on number of rows)
myBoxes = new CheckBox[ds.Tables[0].Rows.Count];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
// Create a new Checkbox
myCheckBox[i] = new CheckBox();
myCheckBox.Location = new Point(xAxisCheckbox, yAxisCheckbox);
myCheckBox.Size = new Size(120, 20);
myCheckBox.Text = ds.Tables[0].Rows[i]["FullName"].ToString();
myCheckBox.Checked = (bool)ds.Tables[0].Rows[i]["InOperation"];
yAxisCheckbox = yAxisCheckbox + 80;
// Add the Checkbox
Controls.Add(myCheckBox[i]);
}
}
private void saveBtn_Click(object sender, EventArgs e) {
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
userID = (int)ds.Tables[0].Rows[i]["UserID"];
if (myBoxes[i].Checked)
{
myconnection.runUpdate("UPDATE Users SET InOperation = 1, OperationOrder = CASE WHEN OperationOrder = 1 THEN 1 ELSE CASE WHEN InOperation=1 THEN OperationOrder ELSE (SELECT COUNT(*)+1 FROM Users WHERE InOperation=1 AND OperationOrder > 0) END END WHERE UserID=" + userID);
myconnection.runUpdate("UPDATE Users SET OperationOrder = CASE WHEN OperationOrder = 0 THEN (SELECT COUNT(*) FROM Users WHERE InOperation=1) ELSE OperationOrder END WHERE InOperation=1");
}
else
{
myconnection.runUpdate("UPDATE Users SET InOperation = 0, OperationOrder = 0 WHERE UserID=" + userID);
myconnection.runUpdate("UPDATE Users SET OperationOrder = CASE WHEN OperationOrder -1 = 0 THEN (SELECT COUNT(*) FROM Users WHERE InOperation=1) ELSE OperationOrder -1 END WHERE InOperation=1");
}
}
}