Click here to Skip to main content
15,884,598 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
VB
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?
Posted
Updated 7-Apr-15 6:40am
v2
Comments
Sergey Alexandrovich Kryukov 7-Apr-15 12:33pm    
To avoid confusions, please add the tag "System.Windows.Forms". Once UI is concerned, always add the tags related to the application type and UI library you want to use; and/or specify full type names.
—SA

1 solution

If it's ok for you to remove the rows not only from being displayed but also from the original DataTable, then do this:
C#
// 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:
C#
// 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;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900