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

Allow And Disallow sorting

1 Answer 61 Views
Grid
This is a migrated thread and some comments may be shown as answers.
johnson lim
Top achievements
Rank 1
johnson lim asked on 17 Dec 2011, 05:32 PM
Hi,
I want to allow sorting in my grid when there is data and disallow sorting when there is no data.
But i find out that once i disallow sorting in NeedDatasource event , it will forever disallow sorting . And i find out that my code in allow/disallow sorting in ItemDataBound event do not work at all.
Below is my code !!
Protected Sub ajaxMsg_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles ajaxMsg.NeedDataSource
        Dim Msg As String
        Dim dsMsg As New DataSet()
        Dim strProjectId As String
 
        Try
 
            Msg = ""
            strProjectId = ddlProjectId.SelectedItem.Text
            dsMsg = getMessageByCompNoAndProjectId(strProjectId, "")
            ajaxMsg.DataSource = dsMsg
             
            If dsMsg.Tables(0).Rows.Count = 0 Then
                ajaxMsg.AllowSorting = False
            Else
                ajaxMsg.AllowSorting = True
            End If
        Catch ex As Exception
            Msg = ex.Message.ToString.Replace("'", "")
            RadAjaxManager1.ResponseScripts.Add("alert('" + Msg + "');")
        End Try
 
    End Sub
     
 
Protected Sub ajaxMsg_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles ajaxMsg.ItemDataBound
 
 
        If TypeOf e.Item Is Telerik.Web.UI.GridFooterItem Then
 
            If ajaxMsg.MasterTableView.Items.Count > 0 Then
 
                If ajaxMsg.EditIndexes.Count > 0 OrElse ajaxMsg.MasterTableView.IsItemInserted Then
 
                    ajaxMsg.MasterTableView.SortExpressions.Clear()
                    ' ajaxMsg.MasterTableView.AllowSorting = False
                    ajaxMsg.AllowSorting = False
 
                Else
 
                    ' ajaxMsg.MasterTableView.AllowSorting = True
                    ajaxMsg.AllowSorting = True
 
                End If
 
            Else
 
                ajaxMsg.MasterTableView.SortExpressions.Clear()
                ajaxMsg.MasterTableView.AllowSorting = False
 
                If ajaxMsg.MasterTableView.NoMasterRecordsText = "" AndAlso Not ajaxMsg.MasterTableView.IsItemInserted Then
 
                    ajaxMsg.MasterTableView.NoMasterRecordsText = Resources.Resource.Search_Display
 
                End If
 
            End If
 
 
        ElseIf TypeOf e.Item Is GridPagerItem Then
 
            ajaxMsg.MasterTableView.PagerStyle.PagerTextFormat = "{2}{3}{4} " + Resources.Resource.Page + " {0} " + Resources.Resource.Pageof + " {1}"
            Dim PageSizeCombo As RadComboBox
            PageSizeCombo = CType(e.Item.FindControl("PageSizeComboBox"), RadComboBox)
            PageSizeCombo.Visible = False
 
 
        ElseIf TypeOf e.Item Is Telerik.Web.UI.GridDataItem AndAlso Not (ajaxMsg.EditIndexes.Count > 0 OrElse ajaxMsg.MasterTableView.IsItemInserted) Then
 
            ajaxMsg.MasterTableView.NoMasterRecordsText = Resources.Resource.Search_Display
            ajaxMsg.MasterTableView.AllowSorting = True
 
            Dim commandItem As GridCommandItem
            commandItem = CType(ajaxMsg.MasterTableView.GetItems(Telerik.Web.UI.GridItemType.CommandItem)(0), GridCommandItem)
            commandItem.FindControl("ExportToCsvButton").Visible = True
 
        End If
 
 
    End Sub

Does anyone has any idea?
HELP Please!!

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 18 Dec 2011, 09:52 AM
Hello,

try to set your logic in Grid's prerender event or Page's Prerender event.
protected void ajaxMsg_PreRender( object sender, EventArgs e )
{
If ajaxMsg.MasterTableView.Items.Count > 0 Then
ajaxMsg.AllowSorting = False
  
                Else
  
                    ajaxMsg.AllowSorting = True
  
                End If
}


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
johnson lim
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or