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

Sorting Problem

5 Answers 134 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 1
Rob asked on 27 Jan 2012, 08:12 PM
RadControls version 2011.3.1305.35

I have a very simple RadGrid example that allows column sorting.  I have added checkboxes to each column header.  I am experiencing 2 issues when attempting to sort.  The 1st, 3rd, 5th, ... (every other time) times that I click on the column header, my checkboxes disappear and the data does not sort.  This is undesired behavior.  The 2nd, 4th, 6th, ... times that I click on the column header, it sorts as expected and my checkboxes are visible as expected.  What I would like to accomplish is:

#1 - Everytime I click on a column header, the grid sorts.  I would expect the SortCommand event to get raised each time.
#2 - Everytime I click on a column header, the checkboxes are visible.

Please watch this video clip to see the what I have decribed as the issue.  I would expect my breakpoint to be hit on every column sort.

http://screencast.com/t/V8OTlSGMdXSL

Here is my code:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="RadGridSorting.aspx.vb" Inherits="RadGridSorting" %>
      
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <telerik:RadScriptManager runat="server" ID="mgrJS">
        </telerik:RadScriptManager>        
        <telerik:RadGrid runat="server" ID="dgHeader" Skin="Web20" AllowSorting="true" >
            <ClientSettings>
                <Scrolling UseStaticHeaders="True" />
                <Selecting EnableDragToSelectRows="False" />
            </ClientSettings>
            <MasterTableView BorderWidth="1px" GridLines="Both" style="border-collapse: collapse !Important;" Font-Bold="false" 
                             TableLayout="Fixed" AutoGenerateColumns="false" ShowHeader="True" >
                <RowIndicatorColumn Visible="False">
                    <HeaderStyle Width="20px" />
                </RowIndicatorColumn>
                <ExpandCollapseColumn Resizable="False" Visible="False">
                    <HeaderStyle Width="20px" />
                </ExpandCollapseColumn>
                <EditFormSettings>
                    <PopUpSettings ScrollBars="None" />
                </EditFormSettings>
            </MasterTableView>
        </telerik:RadGrid>   
    </form>
</body>
</html>
Partial Class RadGridSorting
    Inherits System.Web.UI.Page
  
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim dtHeader As New DataTable
            dtHeader.Columns.Add(New DataColumn("COL_ID", GetType(String)))
            Dim colRow As DataRow = dtHeader.NewRow
            colRow(0) = "1234567"
            dtHeader.Rows.Add(colRow)
            colRow = dtHeader.NewRow
            colRow(0) = "2345678"
            dtHeader.Rows.Add(colRow)
            colRow = dtHeader.NewRow
            colRow(0) = "3456789"
            dtHeader.Rows.Add(colRow)
            colRow = dtHeader.NewRow
            colRow(0) = "4567890"
            dtHeader.Rows.Add(colRow)
            colRow = dtHeader.NewRow
            colRow(0) = "5678901"
            dtHeader.Rows.Add(colRow)
            colRow = dtHeader.NewRow
            colRow(0) = "6789012"
            dtHeader.Rows.Add(colRow)
            colRow = dtHeader.NewRow
            colRow(0) = "7890123"
            dtHeader.Rows.Add(colRow)
            'Define columns of master table view.
            For Each row As DataRow In dtHeader.Rows
                Dim newCol As New GridBoundColumn
                dgHeader.MasterTableView.Columns.Add(newCol)
                newCol.HeaderText = row("COL_ID")
                newCol.DataField = row("COL_ID")
                newCol.DataFormatString = "{0:N0}"
                newCol.HeaderStyle.Width = 100
                newCol.ItemStyle.Width = 100
            Next
            Session("header") = dtHeader
        End If
    End Sub
  
    Protected Sub dgHeader_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles dgHeader.ItemDataBound
        If e.Item.ItemType = GridItemType.Header Then
            Dim hdrItem As GridHeaderItem = CType(e.Item, GridHeaderItem)
            For i As Integer = 2 To hdrItem.Cells.Count - 1
                Dim ctrl As LinkButton = CType(hdrItem.Cells(i).Controls(0), LinkButton)
                If ctrl IsNot Nothing Then
                    Dim val As Integer = 0
                    Integer.TryParse(ctrl.Text, val)
                    If val > 0 Then
                        Dim chk As New CheckBox
                        chk.ID = "chk" & val
                        hdrItem.Cells(i).Controls.Clear()
                        hdrItem.Cells(i).Controls.Add(chk)
                        hdrItem.Cells(i).Controls.Add(ctrl)
                        hdrItem.Cells(i).HorizontalAlign = HorizontalAlign.Center
                    End If
                End If
            Next
  
        End If
    End Sub
  
    Protected Sub dgHeader_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles dgHeader.NeedDataSource
        If Session("data") Is Nothing Then
            Session("data") = CreateTable()
        End If
        dgHeader.DataSource = Session("data")
    End Sub
  
    Private Function CreateTable() As DataTable
        Dim dtHeader As DataTable = Session("header")
        Dim dtData As New DataTable
  
        For Each dr As DataRow In dtHeader.Rows
            dtData.Columns.Add(New DataColumn(dr("COL_ID"), GetType(Integer)))
        Next
  
        'Populate w/ data
        Dim Generator As System.Random = New System.Random()
        Dim r As DataRow
        For i As Integer = 0 To 9
            r = dtData.NewRow
            For j As Integer = 0 To 6
                r(j) = Generator.Next(0, 100)
            Next
            dtData.Rows.Add(r)
        Next
        Return dtData
    End Function
  
    Protected Sub dgHeader_SortCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridSortCommandEventArgs) Handles dgHeader.SortCommand
        Dim s As String = ""
    End Sub
End Class

Thanks.

5 Answers, 1 is accepted

Sort by
0
Rob
Top achievements
Rank 1
answered on 31 Jan 2012, 03:05 PM
I still could really use some help with this issue. Thanks.
0
Shinu
Top achievements
Rank 2
answered on 01 Feb 2012, 05:19 AM
Hello,

Make sure that you have attached the event properly. Here is the sample code.
apsx:
<telerik:RadGrid runat="server" ID="RadGrid1" AllowSorting="true" Skin="Vista" AutoGenerateColumns="true" AllowSorting="true" S OnNeedDataSource="RadGrid1_NeedDataSource"
</telerik:RadGrid>

-Shinu.
0
Accepted
Tsvetoslav
Telerik team
answered on 01 Feb 2012, 03:50 PM
Hi Rob,

Just remove the Controls.Clear() call from your ItemDataBound event and do not readd the link button - just leave as it is.

All the best, Tsvetoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Rob
Top achievements
Rank 1
answered on 01 Feb 2012, 04:59 PM
Shinu,

Thank you for the helpful reply.  From a functionality perspective, your suggestion works perfectly.  From a cosmetic perspective, my client would like the checkbox to appear on the left side of the linkbutton.  Is this possible?  If so, your suggestion how to accomplish this would be greatly appreciated.

Thanks,
Rob
0
Rob
Top achievements
Rank 1
answered on 01 Feb 2012, 05:10 PM
Nevermind.  I figured it out.  I've never used this Controls.AddAt() method before and this worked:

hdrItem.Cells(i).Controls.AddAt(0, chk)

Thank you so much for your help.  I will close this issue.
Tags
Grid
Asked by
Rob
Top achievements
Rank 1
Answers by
Rob
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Tsvetoslav
Telerik team
Share this question
or