如何将selectedItems放在列表的顶部? [英] How to get the selectedItems on top of the list?
问题描述
Hello Experts,
我有一个Windows窗体。我必须创建一个多选项,就像检查下拉列表一样。我想在选中后将所选项目置于顶部。并清除列表的其余部分。可能吗?我不知道如何做到这一点。
这是我的Checkeddropdown列表数据源的代码
Hello Experts,
I have a windows forms. I have to create a multiple selection which is like checked dropdownlist. I want to get the selected Items on the top once it is selected. And clear the rest of the list. Is it possible? I don't have idea about how to do this.
Here is my code for Checkeddropdown list Datasource
Dim dsclass As DataSet = dbDev.GetClass()
CheckedListBox1.DataSource = dsclass.Tables(0)
CheckedListBox1.DisplayMember = "RMCCLS"
CheckedListBox1.ValueMember = "RMCCLS"
之后我检查下拉列表中的方框,我需要将所选项目放在最上面。
如果这个想法不好而且不友好。你可以分享一下这样的想法吗?
After I check the boxes in the drop down list, I need to get the selected items on Top.
If the idea is bad and not amicable. Could you please share any such ideas?
推荐答案
如果您可以删除不仅显示的行而且还可以删除原始DataTable中的行,请执行以下操作:
If it's ok for you to remove the rows not only from being displayed but also from the original DataTable, then do this:
// get the DataTable "back":
DataTable dataTable = ((DataView)CheckedListBox1.DataSource).Table;
// the selected Rows are "in" .CheckedItems in form of DataRowViews:
IEnumerable<DataRow> selectedRows = CheckedListBox1.CheckedItems.Cast<DataRowView>().Select(x => x.Row);
// create a HashSet of the selected DataRows for fast lookup:
HashSet<DataRow> selectedRowsHashset = new HashSet<DataRow>(selectedRows);
// loop over the DataRows in the DataTable in reverse order:
// (required because we're going to remove DataRows)
for (int rowIndex = dataTable.Rows.Count - 1; rowIndex >= 0; rowIndex--)
{
// remove the DataRow at rowIndex if it is not among the selected rows:
if (!selectedRowsHashset.Contains(dataTable.Rows[rowIndex]))
{
dataTable.Rows.RemoveAt(rowIndex);
}
}
如果您只想显示删除的行,请执行以下操作:
If you only want the rows removed from being displayed, then do this:
// get the DataTable "back":
DataTable sourceDataTable = ((DataView)CheckedListBox1.DataSource).Table;
// create a structural clone:
DataTable newDataTable = sourceDataTable.Clone();
// loop over the selected DataRows:
foreach (DataRow sourceRow in CheckedListBox1.CheckedItems.Cast<DataRowView>().Select(x => x.Row))
{
// create a new DataRow and copy the values of the selected DataRow:
DataRow newRow = newDataTable.NewRow();
for (int columnIndex = 0; columnIndex < newDataTable.Columns.Count; columnIndex++)
{
newRow[columnIndex] = sourceRow[columnIndex];
}
// add the DataRow-copy to the new DataTable:
newDataTable.Rows.Add(newRow);
}
// set the new DataTable as new DataSource for the CheckedListBox:
CheckedListBox1.DataSource = newDataTable;
这篇关于如何将selectedItems放在列表的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!