How to filter column on radGrid

6 posts, 0 answers
  1. Caro
    Caro avatar
    34 posts
    Member since:
    Mar 2012

    Posted 07 May 2012 Link to this post

    Hi

    I can't filter column on my radGrid

    <telerik:RadGrid runat="server" ID="grid"    
                           AllowPaging="true"
                           AllowSorting="True"
                           AllowFilteringByColumn="True"
                           ShowStatusBar="true"
                           AutoGenerateColumns="false"
                           Visible="false">
     
                           <ClientSettings >
                               <Selecting AllowRowSelect="True"/>
                               <ClientEvents OnRowDblClick="OnRowDblClick" />
                           </ClientSettings>
     
                           <PagerStyle Mode="NumericPages" Position="Bottom"  />
                           <GroupingSettings CaseSensitive="false" />
        
                           <MasterTableView  ShowHeadersWhenNoRecords="true" DataKeyNames="Name"  ClientDataKeyNames="Name">
     
                           <Columns>
                               <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name"
                               DataType="System.String" AutoPostBackOnFilter="true" SortExpression="Name" >
                               </telerik:GridBoundColumn>
     
                               <telerik:GridBoundColumn DataField="FirstName" UniqueName="FirstName" HeaderText="Firstname"
                               DataType="System.String" AutoPostBackOnFilter="true" SortExpression="Firstname" >
                               </telerik:GridBoundColumn>
     
                               <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="Edit">
                               </telerik:GridEditCommandColumn>
                           </Columns>
                           </MasterTableView>   
                           </telerik:RadGrid>


    protected void button_Click(object sender, EventArgs e)
     {        
                 DataTable dt;
                 SqlConnection conn = new SqlConnection();
                 SqlCommand command = new SqlCommand("fillGrid", cn);
                 command.CommandType = CommandType.StoredProcedure;
     
                 SqlParameter p = new SqlParameter("@param", SqlDbType.NVarChar, 50);
                 p.Value = var.Text;
                 command.Parameters.Add(p);
     
                 dt = new DataTable();
                 SqlDataAdapter da = new SqlDataAdapter(cmd);
     
                 try
                 {
                     conn.Open();
                     da.Fill(dt);
                     grid.DataSource = dt;
                     grid.DataBind();
                 }
                 catch ()
                 {
                     //...
                 }
                 finally
                 {
                     if (conn.State != ConnectionState.Closed)
                     {
                         conn.Close();
                     }
                 }
             }
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 07 May 2012 Link to this post

    Hello Caro,

    From your code, I understood that you are binding the RadGrid on a button click using Simple data binding. In order to implement advanced features like paging, sorting and filtering, you need to bind the grid using NeedDataSource event.
    C#:
    protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
    //code for binding the Radgrid
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        RadGrid1.Visible = true;
        RadGrid1.Rebind();
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Caro
    Caro avatar
    34 posts
    Member since:
    Mar 2012

    Posted 07 May 2012 Link to this post

    Thx Shinu, but filtering still doesn't works
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 08 May 2012 Link to this post

    Hi Caro,

    I tried the same scenario and its working fine at my end. Here is the code i tried.

    ASPX:
    <telerik:RadGrid runat="server" ID="grid" AllowPaging="true" AllowSorting="True" AllowFilteringByColumn="True" ShowStatusBar="true" AutoGenerateColumns="false" Visible="false" onneeddatasource="grid_NeedDataSource">
        <ClientSettings >       
            <Selecting AllowRowSelect="True"/>
        </ClientSettings>
        <PagerStyle Mode="NumericPages" Position="Bottom"  />
        <GroupingSettings CaseSensitive="false" />
        <MasterTableView  ShowHeadersWhenNoRecords="true" DataKeyNames="Name"  ClientDataKeyNames="Name">
            <Columns>
                <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name" DataType="System.String" AutoPostBackOnFilter="true" SortExpression="Name" ></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="FirstName" UniqueName="FirstName" HeaderText="FirstName" DataType="System.String" AutoPostBackOnFilter="true" SortExpression="FirstName" ></telerik:GridBoundColumn>
                <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="Edit"></telerik:GridEditCommandColumn>
            </Columns>
        </MasterTableView>  
    </telerik:RadGrid>
    <asp:Button ID="button1" runat="server" onclick="button1_Click" />

    C#:
    protected void grid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        DataTable dt;
        SqlConnection conn = new SqlConnection();
        SqlCommand command = new SqlCommand("fillGrid", cn);
        command.CommandType = CommandType.StoredProcedure;
      
        SqlParameter p = new SqlParameter("@param", SqlDbType.NVarChar, 50);
        p.Value = var.Text;
        command.Parameters.Add(p);
      
       dt = new DataTable();
       SqlDataAdapter da = new SqlDataAdapter(cmd);
      
       try
       {
           conn.Open();
           da.Fill(dt);
           grid.DataSource = dt;
       }
       catch ()
       {
           //...
       }
       finally
       {
           if (conn.State != ConnectionState.Closed)
           {
               conn.Close();
           }
       }
    }
     
    protected void button1_Click(object sender, EventArgs e)
    {
        grid.Visible = true;
        grid.Rebind();
             
    }

    Please elaborate on the issue if it doesn't help.

    Thanks,
    Shinu.
  6. Caro
    Caro avatar
    34 posts
    Member since:
    Mar 2012

    Posted 08 May 2012 Link to this post

    Hi Shinu

    Thx for your answer.
    I have exactly the same code than you, and, when I click the button filter, nothing happened .... (I don't have the list "No filter", "equal to", "not equal to" etc..)
  7. Caro
    Caro avatar
    34 posts
    Member since:
    Mar 2012

    Posted 08 May 2012 Link to this post

    It's ok, I've found my error :)

    Thx !
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017