aspx:
<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" Skin="Telerik" OnNeedDataSource="RadGrid1_NeedDataSource" OnRowDrop="RadGrid1_RowDrop" AllowMultiRowSelection="true" >
<MasterTableView DataKeyNames="id">
</MasterTableView>
<ClientSettings AllowRowsDragDrop="True">
<Selecting AllowRowSelect="True" EnableDragToSelectRows="true" />
</ClientSettings>
</telerik:RadGrid>
VB:
Protected Sub RadGrid1_RowDrop(ByVal sender As Object, ByVal e As GridDragDropEventArgs) Handles RadGrid1.RowDrop
If String.IsNullOrEmpty(e.HtmlElement) Then
If e.DestDataItem IsNot Nothing Then
Dim categoryList As IList(Of category)
categoryList = categoryStore
Dim categoryItem As category = GetCategory(categoryList, DirectCast(e.DestDataItem.GetDataKeyValue("id"), Integer))
Dim destinationIndex As Integer = categoryList.IndexOf(categoryItem)
Dim categoriesToMove As New List(Of category)()
For Each draggedItem As GridDataItem In e.DraggedItems
Dim tmpcategory As category = GetCategory(categoryList, DirectCast(draggedItem.GetDataKeyValue("id"), Integer))
If tmpcategory IsNot Nothing Then
categoriesToMove.Add(tmpcategory)
End If
Next
For Each categoryToMove As category In categoriesToMove
categoryList.Remove(categoryToMove)
categoryList.Insert(destinationIndex, categoryToMove)
Next
categoryStore = categoryList
RadGrid1.Rebind()
e.DestDataItem.Selected = True
End If
End If
End Sub
Protected Property categoryStore() As IList(Of category)
Get
Try
Dim obj As Object = Session("categoryList_VB")
If obj Is Nothing Then
obj = GetCategories()
Session("categoryList_VB") = obj
End If
Return DirectCast(obj, IList(Of category))
Catch ex As Exception
Session("categoryList_VB") = Nothing
End Try
Return New List(Of category)
End Get
Set(ByVal value As IList(Of category))
Session("categoryList_VB") = value
End Set
End Property
Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As GridNeedDataSourceEventArgs)
RadGrid1.DataSource = categoryStore
End Sub
Protected Function GetCategories() As IList(Of category)
Dim results As IList(Of category) = New List(Of category)()
Dim dbCon As New SqlConnection("Data Source=SHEP\SQLEXPRESS;Initial Catalog=abelSQL;Integrated Security=True")
Dim dbCmd As New SqlCommand
dbCmd.Connection = dbCon
dbCmd.CommandText = "SELECT id, dscrp, seq FROM sitecostsPrimary ORDER BY seq ASC"
dbCon.Open()
Dim reader As IDataReader = dbCmd.ExecuteReader()
While reader.Read()
Dim id As Integer = DirectCast(reader.GetValue(reader.GetOrdinal("id")), Integer)
Dim dscrp As String = DirectCast(reader.GetValue(reader.GetOrdinal("dscrp")), String)
Dim seq As Integer = DirectCast(reader.GetValue(reader.GetOrdinal("seq")), Integer)
results.Add(New category(id, dscrp, seq))
End While
results.Add(New category(999, "Test item", 99))
Return results
End Function
Private Shared Function GetCategory(ByVal ordersToSearchIn As IEnumerable(Of category), ByVal id As Integer) As category
For Each cat As category In ordersToSearchIn
If cat.id = id Then
Return cat
End If
Next
Return Nothing
End Function
Protected Class category
Private _id As Integer
Private _dscrp As String
Private _seq As Integer
Public Sub New(ByVal id As Integer, ByVal dscrp As String, ByVal seq As Integer)
_id = id
_dscrp = dscrp
_seq = seq
End Sub
Public ReadOnly Property id() As Integer
Get
Return _id
End Get
End Property
Public ReadOnly Property dscrp() As String
Get
Return _dscrp
End Get
End Property
Public ReadOnly Property seq() As String
Get
Return _seq
End Get
End Property
End Class