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

Need sample code for Grid Filter Template

1 Answer 131 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aravind
Top achievements
Rank 2
Iron
Iron
Iron
Aravind asked on 21 Jan 2014, 05:19 AM
Hai 
  i need sample code for grid view filter template like this
http://demos.telerik.com/aspnet-ajax/grid/examples/functionality/filtering/filter-templates/defaultvb.aspx

first grid view sample but biding data from server side not in code bhind,bcz i am use Access db,so db path is always change.i need same functionality with filter opation and need one more column with checkbox,after check box need to delete checked box.

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 21 Jan 2014, 09:13 AM
Hi Aravind,

Here is a sample code snippet that shows creating FilterTemplate for a column from code behind:

ASPX:
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
</telerik:RadAjaxLoadingPanel>
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" LoadingPanelID="AjaxLoadingPanel1">
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</telerik:RadAjaxPanel>

VB:
Private dt As DataTable
Private RadGrid1 As RadGrid
 
Private Sub Page_Init(sender As Object, e As System.EventArgs)
    RadGrid1 = New RadGrid()
    RadGrid1.ID = "RadGrid1"
    RadGrid1.AutoGenerateColumns = False
    RadGrid1.AllowPaging = True
    RadGrid1.AllowSorting = True
    RadGrid1.AllowFilteringByColumn = True
    RadGrid1.EnableLinqExpressions = False
    RadGrid1.PagerStyle.AlwaysVisible = True
    RadGrid1.NeedDataSource += New GridNeedDataSourceEventHandler(AddressOf RadGrid1_NeedDataSource)
 
    dt = GetDataTable("SELECT Country FROM Customers")
    Dim ColumnName As String = "Country"
    Dim templateColumn As GridBoundColumn
    templateColumn = New GridBoundColumn()
    templateColumn.FilterTemplate = New MyTemplate(ColumnName, RadGrid1)
    RadGrid1.MasterTableView.Columns.Add(templateColumn)
    templateColumn.DataField = ColumnName
    templateColumn.HeaderText = ColumnName
    templateColumn.UniqueName = ColumnName
 
    Dim boundColumn As GridBoundColumn
    boundColumn = New GridBoundColumn()
    RadGrid1.MasterTableView.Columns.Add(boundColumn)
    boundColumn.DataField = "CompanyName"
    boundColumn.HeaderText = "CompanyName"
 
    boundColumn = New GridBoundColumn()
    RadGrid1.MasterTableView.Columns.Add(boundColumn)
    boundColumn.DataField = "ContactName"
    boundColumn.HeaderText = "Contact Name"
    PlaceHolder1.Controls.Add(RadGrid1)
End Sub
 
Private Class MyTemplate
    Implements ITemplate
    Private dt As DataTable
    Protected combo As RadComboBox
    Private colname As String
    Private radgrid As RadGrid
    Public Sub New(cName As String, grid As RadGrid)
        colname = cName
        radgrid = grid
    End Sub
    Public Sub InstantiateIn(container As System.Web.UI.Control)
        combo = New RadComboBox()
        combo.ID = "RadComboBoxControl"
        combo.DataBinding += New EventHandler(AddressOf RadComboBoxControl_DataBinding)
        container.Controls.Add(combo)
        combo.AutoPostBack = True
        combo.SelectedIndexChanged += New RadComboBoxSelectedIndexChangedEventHandler(AddressOf RadComboBoxControl)
    End Sub
 
    Public Sub RadComboBoxControl_DataBinding(sender As Object, e As EventArgs)
        Dim l As RadComboBox = DirectCast(sender, RadComboBox)
        Dim container As GridFilteringItem = DirectCast(l.NamingContainer, GridFilteringItem)
        l.DataTextField = colname
        l.DataValueField = colname
        dt = GetDataTable("SELECT distinct Country FROM Customers")
        l.DataSource = dt
    End Sub
 
    Private Sub RadComboBoxControl(sender As Object, e As RadComboBoxSelectedIndexChangedEventArgs)
        Dim combo As RadComboBox = TryCast(sender, RadComboBox)
        Dim filterExpression As String
        filterExpression = "([Country] LIKE '" + e.Value + "')"
        radgrid.MasterTableView.FilterExpression = filterExpression
        Dim column As GridColumn = radgrid.MasterTableView.GetColumnSafe(colname)
        column.CurrentFilterFunction = GridKnownFunction.EqualTo
        column.CurrentFilterValue = e.Value
        radgrid.MasterTableView.Rebind()
    End Sub
End Class
 
Protected Sub RadGrid1_NeedDataSource(source As Object, e As GridNeedDataSourceEventArgs)
    dt = GetDataTable("SELECT CompanyName, ContactName, Country FROM Customers")
    Me.RadGrid1.DataSource = dt
End Sub
 
Public Shared Function GetDataTable(query As String) As DataTable
    Dim ConnString As String = ConfigurationManager.ConnectionStrings("Northwind_newConnectionString3").ConnectionString
    Dim conn As New SqlConnection(ConnString)
    Dim adapter As New SqlDataAdapter()
    adapter.SelectCommand = New SqlCommand(query, conn)
 
    Dim myDataTable As New DataTable()
 
    conn.Open()
    Try
        adapter.Fill(myDataTable)
    Finally
        conn.Close()
    End Try
    Return myDataTable
End Function

Thanks,
Shinu
Tags
Grid
Asked by
Aravind
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Shinu
Top achievements
Rank 2
Share this question
or