Hello,
I have a form to show/update Staff information. There is a RadComboBox (CheckBoxes =True, DataSourceID = "sds_multiSites", DataTextField = "Site", DataValueField = "idSite"), that Iist all the Sites in our organization.
When the form loads, I read a table where I find the Staff-MultiSitesSelected. I read all the Sites where the staff works and I check this sites in the RadComboBox. I sort the RadComboBox by the TextValue, using rcb_multiSites.SortItems(New SortCheckedComboItemsFirst()), and this function let me keep the checked items at the top of the list.
Problem: When updating the combo selection, unchecking some items (that are at the top) and selecting new items. After the refresh, the new checked items are wrong even if I don't sort again. The selected index is wrong because is counting the old-checked items that were at the top.
Any suggestions?
cc
ASP.NET
<telerik:RadComboBox ID="rcb_multiSites" Runat="server" DataSourceID="sds_multiSites" DataTextField="Site" DataValueField="idSite" CheckBoxes = "True" EnableCheckAllItemsCheckBox = "true" AutoPostBack = "false" ></telerik:RadComboBox>
VB.NET read the selection from a table and check the selected items in the combo
Private Sub create_new_employee_SaveStateComplete(sender As Object, e As EventArgs) Handles Me.SaveStateComplete
Dim i As Integer ' event launched after load form
If Not IsPostBack Then
Dim conn3 As String = DirectCast(ConfigurationManager.ConnectionStrings("HR_ConnStr").ConnectionString, String)
Dim queryStr3 As String = "SELECT * FROM StaffSites WHERE IDPerson = @IDPerson ORDER BY Name"
Using myConnection3 As New SqlConnection(conn3)
Dim myCommand3 As New SqlCommand(queryStr, myConnection3)
myConnection3.Open()
myCommand3.Parameters.AddWithValue("@IDPDSProfilesPerson", IDPDSProfilesEmployee)
Dim MyReader3 As SqlDataReader = myCommand3.ExecuteReader
If MyReader3.HasRows Then
While (MyReader3.Read())
Dim idSite As String = MyReader3("IDSite")
Dim indexItem As Integer = rcb_multiSites.FindItemIndexByValue(idSite)
rcb_multiSites.Items(indexItem).Checked = True
End While
rcb_multiSites.Sort = RadComboBoxSort.Ascending
rcb_multiSites.SortItems(New SortCheckedComboItemsFirst())
rcb_multiSites.AllowCustomText = True
End If
End Using
End If
End Sub
This function sort the list and keeps the checked Items at the top. The function is from a sample in this website
VB.NET
Public Class SortCheckedComboItemsFirstImplements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim p1 As New RadComboBoxItem()
Dim p2 As New RadComboBoxItem()
If TypeOf x Is RadComboBoxItem Then
p1 = TryCast(x, RadComboBoxItem)
Else
Throw New ArgumentException("Object is not of type RadComboBoxItem.")
End If
If TypeOf y Is RadComboBoxItem Then
p2 = TryCast(y, RadComboBoxItem)
Else
Throw New ArgumentException("Object is not of type RadComboBoxItem.")
End If
Dim cmp As Integer = 0
If p1.Checked Then
If p2.Checked = False Then
Return -1
'uncomment for skipping the sorting of the checked items
'Else 'this line
' Return 1 'this line
End If
ElseIf p2.Checked Then
Return 1
End If
If p1.ComboBoxParent.Sort = RadComboBoxSort.Ascending Then
'here we compare the Values of the items
'cmp = [String].Compare(p1.Value, p2.Value, Not p1.ComboBoxParent.SortCaseSensitive)
cmp = [String].Compare(p1.Text, p2.Text, Not p1.ComboBoxParent.SortCaseSensitive)
End If
If p1.ComboBoxParent.Sort = RadComboBoxSort.Descending Then
'here we compare the Values of the items
'cmp = [String].Compare(p1.Value, p2.Value, Not p1.ComboBoxParent.SortCaseSensitive) * -1
cmp = [String].Compare(p1.Text, p2.Text, Not p1.ComboBoxParent.SortCaseSensitive) * -1
End If
Return cmp
End Function