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

Odd RowIndex Behavior from RadGrid

2 Answers 65 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 07 Sep 2013, 07:23 AM
I'm using the Rad Grid to sort via drag and drop, which is working pretty great. I'm also using a textbox and imagebutton as a second method of sorting my data. To do this I am passing the RowIndex to the ItemCommand then using that rowindex to find my textbox with the new sortID value in it. For some reason it looks like inside the ItemDataBound of the RadGrid The RowIndex gets doubled and it would also seem it uses a 1 based index instead of a 0 based. When i try to find the control using that index it wants to use a zero based index and the doubled index also causes error. I've fixed the problem by dividing the rowindex by 2 and then subtracting 1 for the 0 based index. This solution actually seems to be working fine. However, I don't like using a hack that I don't fully understand. The follow code works, but I'd like to know why i have to divide by 2 and subtract 1 in order for it to work correctly. Look at this line  iRowIndex = (iRowIndex / 2) - 1


Protected Sub rgList_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgList.ItemCommand
 
        If TypeOf e.CommandSource Is ImageButton AndAlso CType(e.CommandSource, ImageButton).ID = "btnSort" Then
 
            Dim iEntityID As Integer = 0
            iEntityID = e.CommandArgument.ToString.Split("|")(0)
 
            Dim iRowIndex As Integer
            iRowIndex = e.CommandArgument.ToString.Split("|")(1)
 
            iRowIndex = (iRowIndex / 2) - 1
 
            Dim iNewSortID As Integer = 0
            Dim txtSortID As TextBox
            txtSortID = rgList.Items(iRowIndex).FindControl("txtSortID")
 
            If IsNumeric(txtSortID.Text) AndAlso txtSortID.Text > 0 Then
                iNewSortID = txtSortID.Text
 
                If iEntityID > 0 Then
                    Dim oItem As New CollectTech.Items.Item(iEntityID)
 
                    If oItem.SortID <> iNewSortID Then
                        oItem.SortID = iNewSortID
                        oItem.UpdateSortID()
                        oItem.Dispose()
                    End If
                End If
            End If
 
            LoadData()
        End If
 
    End Sub
 
    Protected Sub rgList_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles rgList.ItemDataBound
 
        If e.Item.ItemType = Telerik.Web.UI.GridItemType.Item OrElse e.Item.ItemType = Telerik.Web.UI.GridItemType.AlternatingItem Then
 
            Dim oItem As CollectTech.Items.Item = e.Item.DataItem
            Dim litItemName As Literal = e.Item.FindControl("litItemName")
            Dim txtSortID As TextBox = e.Item.FindControl("txtSortID")
            Dim btnSort As ImageButton = e.Item.FindControl("btnSort")
 
            litItemName.Text = Server.HtmlEncode(oItem.Name)
 
            txtSortID.Text = oItem.SortID
 
            btnSort.CommandArgument = oItem.ID & "|" & e.Item.RowIndex
 
            imgThumbnail.ImageUrl = sImagePath
        End If
 
    End Sub
 

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 09 Sep 2013, 05:09 AM
Hi,

Please try setting the 'e.Item.ItemIndex' instead of 'e.Item.RowIndex' as shown below.

VB:
Protected Sub rgList_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles rgList.ItemDataBound
  
        If e.Item.ItemType = Telerik.Web.UI.GridItemType.Item OrElse e.Item.ItemType = Telerik.Web.UI.GridItemType.AlternatingItem Then
            'your code
            btnSort.CommandArgument = oItem.ID & "|" & e.Item.ItemIndex
            . . .
        End If
End Sub

Thanks,
Princy.
0
Dave
Top achievements
Rank 1
answered on 09 Sep 2013, 06:02 AM
That worked. Thanks.
Tags
Grid
Asked by
Dave
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Dave
Top achievements
Rank 1
Share this question
or