复选框列表选择问题
有5个目标和5名员工。每个目标都可以分配给这 5 名员工中的任意数量。因此,我为每个目标设置了 5 个 CheckBoxList,每个 CheckBoxList 都将这 5 名员工的姓名作为项目。
我想从数据库中检索哪些员工已分配了哪些目标。我有以下代码:
List<CheckBoxList> checkboxlists = new List<CheckBoxList>();
checkboxlists.Add(CheckBoxList1);
checkboxlists.Add(CheckBoxList2);
checkboxlists.Add(CheckBoxList3);
checkboxlists.Add(CheckBoxList4);
checkboxlists.Add(CheckBoxList5);
for (int z = 1; z <= checkboxlists.Count; z++)
{
SqlCommand check = new SqlCommand("SELECT ISGoal1, ISGoal2,ISGoal3, ISGoal4,ISGoal5 FROM PRM2011_EMPLOYEE_GOAL WHERE EmployeeID = '" + employeeid[z - 1] + "'", con);
SqlDataReader y = check.ExecuteReader();
y.Read();
for (int j = 1; j <= 5; j++)
{
if (null != y && y.HasRows)
{
string yes_or_no = y["ISGoal" + j].ToString().Trim();
if (yes_or_no == "Yes")
{
checkboxlists[j-1].Items[z-1].Selected = true;
}
//else checkboxlists[j - 1].Items[z - 1].Selected = false;
}
}
y.Close();
}
我的问题是,即使我为一名员工选择一个目标,与该特定员工对应的所有复选框都会被选中。为什么会发生这种情况?
相应地,如果我注释掉发布的代码中的 else 部分,并且未选择任何目标,则与该员工对应的所有复选框都将被取消选中。请帮忙。
There are 5 goals and 5 employees. Each goal can be assigned to any number of these 5 employees. So I have 5 CheckBoxLists for each of the goals, each CheckBoxList having the names of these 5 employees as items.
I want to retrieve from the database which employees have been assigned which goals. I have the following piece of code:
List<CheckBoxList> checkboxlists = new List<CheckBoxList>();
checkboxlists.Add(CheckBoxList1);
checkboxlists.Add(CheckBoxList2);
checkboxlists.Add(CheckBoxList3);
checkboxlists.Add(CheckBoxList4);
checkboxlists.Add(CheckBoxList5);
for (int z = 1; z <= checkboxlists.Count; z++)
{
SqlCommand check = new SqlCommand("SELECT ISGoal1, ISGoal2,ISGoal3, ISGoal4,ISGoal5 FROM PRM2011_EMPLOYEE_GOAL WHERE EmployeeID = '" + employeeid[z - 1] + "'", con);
SqlDataReader y = check.ExecuteReader();
y.Read();
for (int j = 1; j <= 5; j++)
{
if (null != y && y.HasRows)
{
string yes_or_no = y["ISGoal" + j].ToString().Trim();
if (yes_or_no == "Yes")
{
checkboxlists[j-1].Items[z-1].Selected = true;
}
//else checkboxlists[j - 1].Items[z - 1].Selected = false;
}
}
y.Close();
}
My problem is that even if I select one goal for an employee, all the checkboxes corresponding to that particular employee get selected. Why is this happening?
Correspondingly, if I comment out the else portion in the code posted, and if any of the goals are not selected, then all the checkboxes corresponding to that employee goes unselected. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些想法:
checkboxlists.Count
以及从 0 到小于 5。这样您就可以避免在各处处理所有减法。checkboxlists[j-1].Items[z-1].Selected = true
行上,不应该是checkboxlists[z-1].Items[j-1]。 Selected = true
因为我假设您正在使用 z 来迭代您的 CheckBoxLists。现在已经很晚了,所以我的大脑可能有点模糊,但似乎#2 可能是你的问题。尝试一下这些想法,如果您仍然遇到问题,我会与您联系。
A few thoughts:
checkboxlists.Count
and from 0 to less than 5. That way you can avoid having to deal with all the subtractions everywhere.checkboxlists[j-1].Items[z-1].Selected = true
, shouldn't it becheckboxlists[z-1].Items[j-1].Selected = true
since I am assuming you are using z to iterate over your CheckBoxLists.Its late here, so my brain may be a bit fuzzy, but it seems that #2 could be your issue. Give those thoughts a shot and I will follow up with you if you are still having issues.
您的代码中有很多问题,我将告诉您主要问题,然后列出代码的其他缺陷。
造成这种情况的原因是:
for 循环外部存在方法
y.Read();
。因为
Read();
的功能是读取数据库中的下一行。所以基本上你的代码读取第一个值,让我们假设该值为“Yes”,这样它将导致 ListBox 检查员工,而不是再次调用 y.Read(); 以便它移动到下一行,它不会! ..因此该值保持为“是”,因此列表中的所有复选框都将被选中。解决方案:
很简单,只需将
y.Read();
从循环外部移入循环即可。像这样:
关于代码的额外注释
首先
您需要编辑
SqlCommand
以使用SqlParameters
第二
如果如果你正在尝试构建一个真正的应用程序,那么这是一个非常糟糕的做法。即使您没有真正构建这个应用程序,我也不认为这是练习的方式。
您的循环应如下所示:
Third
使用字符串来表示是/否值也是一种不好的做法。您应该将其用于所有
ISGoal
列数据库,数据类型BIT
。因此,您需要将 C# 代码中的局部变量的 DateType 从string
更改为bool
。第四
你应该像 Ryan 所说的那样进行切换,因为
z
表示CheckBoxLists
,j
表示 a 的项目给定CheckBoxList
所以它可能是这样的:
注意:我一开始没有注意,我认为 [z-1] 是某种 LINQ 表达式 :D!! ..这是我的错,但我的意思是当我第一次开始编程时我曾经这样做过,但我仍然无法识别它..这不是我认为的最佳实践! ..我的建议尊重使用的从零开始的编号。
最后
您不必每次都检查 y.Read(); 是否有效。是否为空。所以我认为这段代码更有意义,而且对循环的条件进行硬编码是一种非常糟糕的做法,因此我们将使用 while 循环并添加 int 类型的局部变量,然后我们将在我们的内部递增它循环代码,以便您可以使用它来访问 CheckBoxList 项目
..祝你好运;)
You have many issues in your code I'll tell you the main problem and then I'll list the other flaws of your code.
What's causing this is:
the existence of the method
y.Read();
out side of the for loop.since the function of
Read();
is to read the next row in the database. so basically your code reads the first value, Let's assume the value will be "Yes" so it will cause the ListBox to check the employee and instead of calling they.Read();
again so it moves to the next row it DOES NOT! .. so the value is kept "Yes" and hense all the CheckBoxes in the List will be checked.The Solution:
It's as simple as just moving the
y.Read();
from out side the loop into it.Like this:
Extra Notes on your code
First of all
You need to edit your
SqlCommand
to useSqlParameters
Second
If you are trying to build a real application then this is a very bad practice. Even if you are not building this application for real I don't think this is the way to practice.
your Loop should look like this:
Third
Using a string to represent a Yes/No value is a also a bad practice .. You should use for all your
ISGoal
columns database, the DataTypeBIT
. and consequently you'll change the local variable's DateType in your C# code fromstring
tobool
.Fourth
You should switch as Ryan said, because
z
denotes to theCheckBoxLists
andj
denotes to the items of a givenCheckBoxList
so it could be like this:
N.B: I wasn't paying attention at first I thougth [z-1] is some kind of LINQ expression :D!! .. It's my fault but I mean I used to do this when I first started programming and I still couldn't recognize it .. It's not the best practice I think! .. my advice respect the used Zero-Based numbering.
Finally
You don't have to check everytime if y.Read(); is null or not. So I think this piece of code would make more sense and also hard coding the condition of the loop is a very bad practice, so we'll use a while loop and add local variable of type int, then we'll increment it within our loop code so you could use it to access the CheckBoxList items
..Good Luck ;)