Hello,
I am working on a project with RadGrid.
I am trying to apply Google-like Filtering on my project.
(http://www.telerik.com/help/aspnet-ajax/grid-google-like-filtering.html)
I have some problems:
1.) After I filter a column, I don’t see any data in my grid.
2.) Is it possible to filter a numeric field that I do not know in advance its name? (data source of my grid is dynamic and I don’t know in advance the type of each column)
My code so far:
Thanks,
Daniel.
I am working on a project with RadGrid.
I am trying to apply Google-like Filtering on my project.
(http://www.telerik.com/help/aspnet-ajax/grid-google-like-filtering.html)
I have some problems:
1.) After I filter a column, I don’t see any data in my grid.
2.) Is it possible to filter a numeric field that I do not know in advance its name? (data source of my grid is dynamic and I don’t know in advance the type of each column)
My code so far:
001.Public Class Google_Like_Filter2002. Inherits System.Web.UI.Page003. Dim dd As New DummyData004. Dim dt As New DataTable005. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load006. If Not IsPostBack Then007. dt = dd.GenTableData2008. Me.RadGrid1.MasterTableView.Columns.Clear()009. 010. For Each dataColumn As DataColumn In dt.Columns011. Dim gridColumn As New MyCustomFilteringColumnVB()012. Me.RadGrid1.MasterTableView.Columns.Add(gridColumn)013. gridColumn.DataField = dataColumn.ColumnName014. gridColumn.HeaderText = dataColumn.ColumnName015. Next016. End If017. End Sub018. 019. Private Sub RadGrid1_ColumnCreating(sender As Object, e As Telerik.Web.UI.GridColumnCreatingEventArgs) Handles RadGrid1.ColumnCreating020. If (e.ColumnType = GetType(MyCustomFilteringColumnVB).Name) Then021. e.Column = New MyCustomFilteringColumnVB()022. End If023. End Sub024. 025. Private Sub RadGrid1_ItemCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand026. If (e.CommandName = "Filter") Then027. For Each column As GridColumn In e.Item.OwnerTableView.Columns028. column.CurrentFilterValue = String.Empty029. column.CurrentFilterFunction = GridKnownFunction.NoFilter030. Next031. End If032. End Sub033. 034. Private Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource035. RadGrid1.DataSource = dt036. 037. End Sub038.End Class039. 040.Public Class MyCustomFilteringColumnVB041. Inherits GridBoundColumn042. Dim dd As New DummyData043. 'RadGrid will call this method when it initializes the controls inside the filtering item cells044. Protected Overloads Overrides Sub SetupFilterControls(ByVal cell As TableCell)045. MyBase.SetupFilterControls(cell)046. cell.Controls.RemoveAt(0)047. Dim combo As New RadComboBox()048. combo.ID = ("RadComboBox1" + Me.UniqueName)049. combo.ShowToggleImage = False050. combo.Skin = "Office2007"051. combo.EnableLoadOnDemand = True052. combo.AutoPostBack = True053. combo.MarkFirstMatch = False054. combo.Height = Unit.Pixel(100)055. combo.EnableAutomaticLoadOnDemand = False056. AddHandler combo.ItemsRequested, AddressOf Me.list_ItemsRequested057. AddHandler combo.SelectedIndexChanged, AddressOf Me.list_SelectedIndexChanged058. cell.Controls.AddAt(0, combo)059. cell.Controls.RemoveAt(1)060. End Sub061. 'RadGrid will cal this method when the value should be set to the filtering input control(s)062. Protected Overloads Overrides Sub SetCurrentFilterValueToControl(ByVal cell As TableCell)063. MyBase.SetCurrentFilterValueToControl(cell)064. Dim combo As RadComboBox = DirectCast(cell.Controls(0), RadComboBox)065. If (Me.CurrentFilterValue <> String.Empty) Then066. combo.Text = Me.CurrentFilterValue067. End If068. End Sub069. 'RadGrid will cal this method when the filtering value should be extracted from the filtering input control(s)070. Protected Overloads Overrides Function GetCurrentFilterValueFromControl(ByVal cell As TableCell) As String071. Dim combo As RadComboBox = DirectCast(cell.Controls(0), RadComboBox)072. Return combo.Text073. End Function074. Private Sub list_ItemsRequested(ByVal o As Object, ByVal e As RadComboBoxItemsRequestedEventArgs)075. Dim dt As DataTable = dd.GenTableData2076. CType(o, RadComboBox).DataTextField = Me.DataField077. CType(o, RadComboBox).DataValueField = Me.DataField078. 'If (Me.ColumnType Is GetType(String)) Then079. 080. 'dt = dt.Select(String.Format(Me.UniqueName + " LIKE '*{0}*'", e.Text)).CopyToDataTable081. dt = dt.Select(Me.UniqueName + " LIKE '%" + e.Text + "%'").CopyToDataTable082. Dim dv As DataView = New DataView(dt)083. dt = dv.ToTable("dt", False, Me.UniqueName)084. ''Else085. 'End If086. 087. 088. 089. CType(o, RadComboBox).DataSource = dt090. 091. CType(o, RadComboBox).DataBind()092. End Sub093. 094. Private Sub list_SelectedIndexChanged(ByVal o As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)095. Dim filterItem As GridFilteringItem = DirectCast((DirectCast(o, RadComboBox)).NamingContainer, GridFilteringItem)096. If (Me.UniqueName = "Index") Then097. 'If (Me.ColumnType Is GetType(Integer)) Then098. 'this is filtering for integer column type099. filterItem.FireCommandEvent("Filter", New Pair("EqualTo", Me.UniqueName))100. End If101. 'filtering for string column type102. filterItem.FireCommandEvent("Filter", New Pair("Contains", Me.UniqueName))103. End Sub104.End Class105. 106.Public Class DummyData107. Public Function GenTableData2(Optional records = 30)108. Dim dt As New DataTable109. dt.Columns.Add("ID", GetType(Integer))110. dt.Columns.Add("First", GetType(String))111. dt.Columns.Add("Last", GetType(String))112. dt.Columns.Add("Birth", GetType(DateTime))113. 114. 115. For index = 1 To records116. dt.Rows.Add(index, "First" & index, "Last" & index, Date.Now.AddDays(-index))117. Next118. 119. Return dt120. End Function121.End ClassThanks,
Daniel.
