筛选级联CheckedListBox并保留项目的检查状态 [英] Filter cascading CheckedListBox and preserve check state of the items

查看:102
本文介绍了筛选级联CheckedListBox并保留项目的检查状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经问过这个问题,几天前,答案已经很好地解决了我的问题。我自己的帖子还有另一个相关问题。

I have asked This Question a few days ago and the answer has solved my problem perfectly. I have another related question to my own post.

秒数的 CheckState (医生) Checkedlistbox 通过更改第一个(特殊) Checkedlistbox 项来更改数据源时,这些项将被保存。这是我的代码:

The CheckState of second (Doctors') Checkedlistbox items will be saved when I change data source by checking the first (Specialty) Checkedlistbox items. Here's my code:

CheckedListBoxItem 类:

/// <summary>
/// A class to svae status of checkboxes when datasource changes.
/// </summary>
/// <typeparam name="T"></typeparam>
public class CheckedListBoxItem<T>
{
    public CheckedListBoxItem(T item)
    {
        DataBoundItem = item;
    }
    public T DataBoundItem { get; set; }
    public CheckState CheckState { get; set; }
    public override string ToString() { return DataBoundItem.ToString(); }
}

SpecialityCheckList 和与第一个和第二个 Checkedlistbox 控件相关的 DoctorCheckList

SpecialityCheckList and DoctorCheckList related to first and second Checkedlistbox controls:

public class SpecialityCheckList
{
    public int SpecialtyID { get; set; }
    public string Title { get; set; }
    public override string ToString() { return Title; }
}

public class DoctorCheckList
{
    public int DoctorID { get; set; }
    public string Name { get; set; }
    public int? SpecialityId { get; set; }
    public override string ToString() { return Name; }
}

表单代码:

BindingList<CheckedListBoxItem<DoctorCheckList>> doctors = new BindingList<CheckedListBoxItem<DoctorCheckList>>();

private void ReportVisitSocialInsurance_Load(object sender, EventArgs e)
{
     SpecialtyTypeCheckbox.DataSource = new BindingList<SpecialityCheckList>(_Specialty.SelectTbl_SpecialityCheckListBox());

     DoctorsIDCheckedlistbox.DataSource = doctors;
     doctors.ListChanged += Doctors_ListChanged;
}

private void Doctors_ListChanged(object sender, ListChangedEventArgs e)
{
     for (var i = 0; i < DoctorsIDCheckedlistbox.Items.Count; i++)
     {
         DoctorsIDCheckedlistbox.SetItemCheckState(i, ((CheckedListBoxItem<DoctorCheckList>)DoctorsIDCheckedlistbox.Items[i]).CheckState);
     }
}

private void SpecialtyTypeID_ItemCheck(object sender, ItemCheckEventArgs e)
{
     var item = (SpecialityCheckList)SpecialtyTypeCheckbox.Items[e.Index];
     if (e.NewValue == CheckState.Checked)
     {
          _Doctors.SelectTbl_DoctorsCheckListBox(item.SpecialtyID)
                    .Select(s => new CheckedListBoxItem<DoctorCheckList>(s)).ToList()
                    .ForEach(f => doctors.Add(f));
     } else {
          doctors
                    .Where(w => w.DataBoundItem.SpecialityId == item.SpecialtyID)
                    .ToList()
                    .ForEach(f => doctors.Remove(f));
     }
}

private void DoctorsID_ItemCheck(object sender, ItemCheckEventArgs e)
{
      ((CheckedListBoxItem<DoctorCheckList>)DoctorsIDCheckedlistbox.Items[e.Index]).CheckState = e.NewValue;
}

但是我如何搜索Doctors' Checkedlistbox 项并检查一项,当我清除搜索文本框以查看所有医生的姓名时,所检查的项与我在搜索时所检查的项不同。这是因为它使用了我认为的索引。这是我搜索时的代码:

How ever if I search through Doctors' Checkedlistbox items and check a item, when I clear search textbox to see all doctors' name the checked item is different from what I have checked while searching. This is because it uses index I think. Here is my code when searching:

private void DoctorsNameTextbox_TextChanged(object sender, EventArgs e)
{
     BindingList<CheckedListBoxItem<DoctorCheckList>> doctorsSerach =
                ObjectCloning.CloneJson<BindingList<CheckedListBoxItem<DoctorCheckList>>>(doctors);

     doctorsSerach
                .Where(w => !w.DataBoundItem.Name.Contains(DoctorsNameTextbox.Text))
                .ToList()
                .ForEach(f => doctorsSerach.Remove(f));

     DoctorsIDCheckedlistbox.DataSource = doctorsSerach;
}

问题是,例如,如果我搜索姓名Ali,它将显示我3项当我检查项目编号2nd并清除搜索文本框时,已经检查了索引1(从零开始)的项目。

The problem is that for example if I search for name Ali, it shows me 3 items. When I check item number 2nd and clear search textbox, item at index 1 (zero-based) has been checked.

推荐答案

何时处理 searchTextBox TexctChanged 事件,您可以检查文本是否为空,将数据源设置为医生列表,否则将数据源设置为已过滤医生的列表。在这两种情况下,设置数据源后,都将复选标记与数据源同步:

When handling TexctChanged event of the searchTextBox, you can check if the text is empty, set the data source to list of doctors, otherwise set the data source to a list of filtered doctors. In both case, after setting the data source, sync the check marks with data source:

private void searchTextBox_TextChanged(object sender, EventArgs e)
{
    if(string.IsNullOrEmpty(searchTextBox.Text))
    {
        doctorsCheckedListBox.DataSource = doctors;
    }
    else
    {
        var filteredDoctors = 
            new BindingList<CheckedListBoxItem<Doctor>>
            (doctors.Where(x => x.DataBoundItem.Name.StartsWith(searchTextBox.Text))
            .ToList());
        doctorsCheckedListBox.DataSource = filteredDoctors;
    }
    for (var i = 0; i < doctorsCheckedListBox.Items.Count; i++)
    {
        doctorsCheckedListBox.SetItemCheckState(i,
            ((CheckedListBoxItem<Doctor>)doctorsCheckedListBox.Items[i]).CheckState);
    }
}

这篇关于筛选级联CheckedListBox并保留项目的检查状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆