This is a migrated thread and some comments may be shown as answers.

[Solved] RadGrid AJAX drag and drop rows

2 Answers 343 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 28 Apr 2008, 12:17 PM
Hi, I'm having problems trying to get drag and drop working on a simple radgrid. I've based my code on the website demo as follows - cab you help please?

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

2 Answers, 1 is accepted

Sort by
0
David
Top achievements
Rank 1
answered on 28 Apr 2008, 05:17 PM
Sorry, I should have explained what the problem is - everything seems to work fine, but the grid is not changed after a row is dragged and dropped. Debug shows that the rowdrop event args  has a null destination row id ( I have tried dropping the row on lots of different parts of the grid but it is always null)

Server is IS6 on XP, .Net 3.5, Visual Studio 2008, IE7.

Any pointers would be much appreciated, as I cannot see whats wrong.
0
Yavor
Telerik team
answered on 01 May 2008, 12:44 PM
Hi David,

Based on the supplied information, it is hard to determine the cause of the problem, without having a sample which can be debugged. Therefore, it would be best, if you open a formal support ticket, and send us the problematic code, in the form of a working sample. We will review it locally, and get back to you.

Greetings,
Yavor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
David
Top achievements
Rank 1
Yavor
Telerik team
Share this question
or