如何在 C# 中获取列表视图中的选中项?
How to get the checkeditems in a listview in C#?
我有一个列表视图,用户可以检查列表视图中的多个项目,代码会将列表视图中检查的值与 MS Access 数据库中 table 中的值进行比较。
问题是,如果用户检查的值是 X,它会变成 {Text="X"} 并与数据库中的值进行比较,这是错误的。
有谁知道发生了什么事?
这就是我获取选中项目的方式:
foreach (Listview module in listView1.CheckedItems)
{
if(module.ToString() == Convert.ToString((test.Tables["objects"].Rows[i]["objectname"])))
}
简单解释一下我是如何将项目添加到列表视图中的:
我的数据库中有一个 table,列表视图中的项目来自该 table 的列之一。但我需要 table 基于例如列 [1] 进行排序,然后将列 [2] 中的值导出到列表视图。我还将这些项目分为三组。
ListViewGroup Small_Modules = new ListViewGroup("Small Modules", HorizontalAlignment.Left);
ListViewGroup Medium_Modules = new ListViewGroup("Midum Modules", HorizontalAlignment.Left);
ListViewGroup Heavy_Modules = new ListViewGroup("Heavy Modules", HorizontalAlignment.Left);
//find the maximum moment in the column
//this maximum basically does nothing and it was just for future refrencess
double max_moment = 0;
int i = 0, j = 0;
foreach (DataRow item in test.Tables["objects"].Rows)
{
//i is defined to count the number of modules in the table
i++;
//Proj_ID is a user input through a textbox
if (proj_ID == Convert.ToInt32(item["projID"]))
{
if (max_moment < Convert.ToDouble(item["moment"]))
{
max_moment = Convert.ToDouble(item["moment"]);
}
}
}
//sort the table based on the moment in an ascending order
DataView dv = test.Tables["objects"].DefaultView;
dv.Sort = "moment ASC";
DataTable sorted_dt = dv.ToTable();
//define each group members
foreach (DataRow row in sorted_dt.Rows)
{
//First one third of the items goes into group one
//Second one third of the items goes into group two
//last one third of the items goes into group three
j++;
//Proj_ID is a user input through a textbox
if (proj_ID == Convert.ToInt32(row["projID"]))
{
if (j <= i / 3)
{
listView1.Items.Add(new ListViewItem(Convert.ToString(row["objectname"]), Small_Modules));
}
else if (j <= i * 2 / 3 && j > i / 3)
{
listView1.Items.Add(new ListViewItem(Convert.ToString(row["objectname"]), Medium_Modules));
}
else if (j > i * 2 / 3)
{
listView1.Items.Add(new ListViewItem(Convert.ToString(row["objectname"]), Heavy_Modules));
}
}
}
listView1.Groups.Add(Small_Modules);
listView1.Groups.Add(Medium_Modules);
listView1.Groups.Add(Heavy_Modules);
我正在使用 C#WFA
我的数据库是 MS Access
好的,我很惊讶没有人知道这个问题的答案。我可以通过逐行调试代码并检查幕后发生的事情来找到答案。
使用列表视图时,项目将保存为 {Text = "X"},而在列表视图中我们只有 X。为了在我的问题中访问 X,我不得不使用 module.Text 而不是只有模块。
foreach (Listview module in listView1.CheckedItems)
{
if(module.Text.ToString() == Convert.ToString((test.Tables["objects"].Rows[i]["objectname"])))
}
我有一个列表视图,用户可以检查列表视图中的多个项目,代码会将列表视图中检查的值与 MS Access 数据库中 table 中的值进行比较。 问题是,如果用户检查的值是 X,它会变成 {Text="X"} 并与数据库中的值进行比较,这是错误的。 有谁知道发生了什么事? 这就是我获取选中项目的方式:
foreach (Listview module in listView1.CheckedItems)
{
if(module.ToString() == Convert.ToString((test.Tables["objects"].Rows[i]["objectname"])))
}
简单解释一下我是如何将项目添加到列表视图中的: 我的数据库中有一个 table,列表视图中的项目来自该 table 的列之一。但我需要 table 基于例如列 [1] 进行排序,然后将列 [2] 中的值导出到列表视图。我还将这些项目分为三组。
ListViewGroup Small_Modules = new ListViewGroup("Small Modules", HorizontalAlignment.Left);
ListViewGroup Medium_Modules = new ListViewGroup("Midum Modules", HorizontalAlignment.Left);
ListViewGroup Heavy_Modules = new ListViewGroup("Heavy Modules", HorizontalAlignment.Left);
//find the maximum moment in the column
//this maximum basically does nothing and it was just for future refrencess
double max_moment = 0;
int i = 0, j = 0;
foreach (DataRow item in test.Tables["objects"].Rows)
{
//i is defined to count the number of modules in the table
i++;
//Proj_ID is a user input through a textbox
if (proj_ID == Convert.ToInt32(item["projID"]))
{
if (max_moment < Convert.ToDouble(item["moment"]))
{
max_moment = Convert.ToDouble(item["moment"]);
}
}
}
//sort the table based on the moment in an ascending order
DataView dv = test.Tables["objects"].DefaultView;
dv.Sort = "moment ASC";
DataTable sorted_dt = dv.ToTable();
//define each group members
foreach (DataRow row in sorted_dt.Rows)
{
//First one third of the items goes into group one
//Second one third of the items goes into group two
//last one third of the items goes into group three
j++;
//Proj_ID is a user input through a textbox
if (proj_ID == Convert.ToInt32(row["projID"]))
{
if (j <= i / 3)
{
listView1.Items.Add(new ListViewItem(Convert.ToString(row["objectname"]), Small_Modules));
}
else if (j <= i * 2 / 3 && j > i / 3)
{
listView1.Items.Add(new ListViewItem(Convert.ToString(row["objectname"]), Medium_Modules));
}
else if (j > i * 2 / 3)
{
listView1.Items.Add(new ListViewItem(Convert.ToString(row["objectname"]), Heavy_Modules));
}
}
}
listView1.Groups.Add(Small_Modules);
listView1.Groups.Add(Medium_Modules);
listView1.Groups.Add(Heavy_Modules);
我正在使用 C#WFA 我的数据库是 MS Access
好的,我很惊讶没有人知道这个问题的答案。我可以通过逐行调试代码并检查幕后发生的事情来找到答案。
使用列表视图时,项目将保存为 {Text = "X"},而在列表视图中我们只有 X。为了在我的问题中访问 X,我不得不使用 module.Text 而不是只有模块。
foreach (Listview module in listView1.CheckedItems)
{
if(module.Text.ToString() == Convert.ToString((test.Tables["objects"].Rows[i]["objectname"])))
}