如何获取 CheckedListBox 中的项目 ID?
我有一个方法,可以从数据库中提取 url 名称(varchar)、urlID(int)及其启用状态(位),并将结果填充到 foreach 循环上的 CheckedListBox 中。我遇到的问题是 checkboxlist 似乎只采用名称及其检查状态。我需要做的是,当用户完成按钮事件上的选择时,它会读取 CheckedListBox 并获取 URL ID 和启用状态,以便我可以将其写回数据库。
这是我正在使用的代码:
/// <summary>
/// Create url names in check box list.
/// </summary>
/// <param name="rows"></param>
private void CreateURLCheckBoxes(DataRowCollection rows)
{
try
{
int i = 0;
foreach (DataRow row in rows)
{
//Gets the url name and path when the status is enabled. The status of Enabled / Disabled is setup in the users option page
string URLName = row["URLName"].ToString();
int URLID = Convert.ToInt32(row["URLID"]);
bool enabled = Convert.ToBoolean(row["Enabled"]);
//Adds the returned to rows to the check box list
CBLUrls.Items.Add(URLName, enabled);
}
i++;
}
catch (Exception ex)
{
//Log error
Functionality func = new Functionality();
func.LogError(ex);
//Error message the user will see
string FriendlyError = "There has been populating checkboxes with the urls ";
Classes.ShowMessageBox.MsgBox(FriendlyError, "There has been an Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
I have a method that pulls a url name(varchar), a urlID(int) and its Enabled status(bit) from a database and populates the results to a CheckedListBox on a foreach loop. The problem I have is the checkedboxlist only seems to take a name and its checked status. What i need to be able to do is when a user has finished with there selections on a button event it reads CheckedListBox and gets the URL ID, and enabled status so I can then write this back to the database.
This is the code I am using:
/// <summary>
/// Create url names in check box list.
/// </summary>
/// <param name="rows"></param>
private void CreateURLCheckBoxes(DataRowCollection rows)
{
try
{
int i = 0;
foreach (DataRow row in rows)
{
//Gets the url name and path when the status is enabled. The status of Enabled / Disabled is setup in the users option page
string URLName = row["URLName"].ToString();
int URLID = Convert.ToInt32(row["URLID"]);
bool enabled = Convert.ToBoolean(row["Enabled"]);
//Adds the returned to rows to the check box list
CBLUrls.Items.Add(URLName, enabled);
}
i++;
}
catch (Exception ex)
{
//Log error
Functionality func = new Functionality();
func.LogError(ex);
//Error message the user will see
string FriendlyError = "There has been populating checkboxes with the urls ";
Classes.ShowMessageBox.MsgBox(FriendlyError, "There has been an Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
步骤 1:创建一个类来保存名称和 Id,并覆盖返回名称的 ToString()
步骤 2:将此类的实例添加到 CheckedListBox
步骤 3:将 SelectedItem 转换为 UrlInfo 并检索 .Id
Step 1: Create a class to hold the Name and Id with a ToString() override that returns the Name
Step 2: Add instances of this class to your CheckedListBox
Step 3: Cast SelectedItem to UrlInfo and retrieve the .Id
您最好创建一个包含
string
(您的 url)和int
(id)的简单类,覆盖ToString()
方法返回 url,并将这些对象添加到CheckedListBox
的Items
属性中。当您获取选定的对象时,只需将其转换为新类,然后就可以访问这两个属性。
像这样:
然后当您添加对象时:
You'd better create a simple class containing a
string
(your url) and anint
(the id), override theToString()
method to return the url, and add those objects to theItems
property of theCheckedListBox
.When you get the selected object, you just have to cast it into your new class, and you can access both properties.
Something like:
And then when you add the objects :
该控件有一个 Value 成员和一个 Display 成员。我认为如果您使用 Name 作为显示成员并将 ID 作为值成员,您就可以做您需要的事情。
This control has a Value Member and a Display member. I think you can do what you need if you use the Name as the display member and the ID as the value member.