[英]C# listview checked box
我正在尝试获取刚刚被检查的listview项的索引,并基于刚刚被检查的项更新数据库,而不考虑在尝试使用复选框指示用户想要通知之前已检查的其他项是否,所以当用户选中复选框时,我想使用该项目的索引并将该项目的通知设置为true,但我只能一次获取所有checkeditems索引。
请帮忙。
我能够调用itemcheck事件函数,但它会考虑初始检查的项目以及用户检查的项目。 我设法使用布尔函数“ Item_checked by user”分离了最初检查的项目
` private static bool checked_by_user;
private void courseworks_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (checked_by_user == true)
{ //do something
}
else ;//do nothing
}`
现在,我希望能够仅获取刚刚检查的行的bar_ref_id,我的列表视图是从数据库中创建的,如下所示
foreach (var item2 in CW_query2)//for each CW
{
if (item.Status == true)
{
ListViewItem items = new ListViewItem(new[] {//adds items into list view, per column
item2.Module_Code, item2.Title, item2.Due_Date.ToString("dd/MM/yy"),"Submitted",item.Bar_Ref_ID
});
courseworks.Items.Add(items);
}
else
{
ListViewItem items = new ListViewItem(new[] {//adds items into list view, per column
item2.Module_Code, item2.Title, item2.Due_Date.ToString("dd/MM/yy"),"Not-Submitted",item.Bar_Ref_ID
});
courseworks.Items.Add(items);
}
希望我添加的信息有帮助。 提前致谢
如果您已经从数据库中获取订阅并根据用户的订阅为每个项目设置Checked属性,那么使用复选框的CheckedChanged事件不是最简单的吗? 很难说出实现方式是什么,但是当框未选中时(删除订阅),应该有一个功能;选中框时(添加订阅),则应该具有另一个功能。
如果您能够提供一些代码,我可能会更加具体。
在您的ItemChecked事件中,.NET将object sender
和ItemCheckEventArgs e
公开为事件的参数。 在该函数内,您可以查看sender
以获取已选中/未选中的ListViewItem,还可以查看e
以检索该项目在ListView中的索引(以防您可以使用索引轻松更改其中的数据)您的数据库)。 这是一个简短的示例,我几乎将直接从Microsoft窃取:
private void ListView1_ItemCheck1(object sender, ItemCheckEventArgs e)
{
ListViewItem item = (ListViewItem)sender
if (e.CurrentValue==CheckState.Unchecked)
{
Unsubscribe(e.Index, currentUserID);
/*You can pass the Index of the ListViewItem that caused the event
to a method that will update your database (I would find it easier
to program a function that takes the current user's ID as a parameter)*/
Unsubscribe(item.Name, currentUserID);
/*OR this might be a better way for you to reference what subscription
should be cancelled (again, in addition to a UserID)*/
}
else if((e.CurrentValue==CheckState.Checked))
{
Subscribe(e.Index, currentUserID);
}
}
private void Unsubscribe(int index, int userID)
{
//unsubscribe the referenced userID from the subscription at index
}
private void Unsubscribe(string subscriptionName, int userID)
{
//unsubscribe the referenced userID from the subscription called subscriptionName
}
我无法为您的第二段代码提供更具体的示例,因为我不确定它在做什么。 看起来您在做的事情可能比上面的代码示例所能处理的还要复杂,但是也许代码会为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.