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

Apply filter programmatically

4 Answers 243 Views
Grid
This is a migrated thread and some comments may be shown as answers.
July
Top achievements
Rank 2
July asked on 16 Feb 2012, 02:12 PM

I have a problem with Date Filter.

This is my Radgrid



When I filter wiht 15/02/2012 --> does not return nothing.
I need SHOW DATE + HOUR, is important!
.. I try this


  protected void ActivityLog_ItemCommand(object sender, GridCommandEventArgs e)
         {
             if (e.CommandName == RadGrid.FilterCommandName)
             {
                 Pair filterPair = e.CommandArgument as Pair;
                 string columnName = Convert.ToString(filterPair.Second);
                 if (columnName == "Date")
                 {
                     e.Canceled = true;

                     string date =
                         ((RadDatePicker)((GridFilteringItem)e.Item)[Convert.ToString(filterPair.Second)].Controls[0])
                             .SelectedDate.ToString();
                     DateTime startDate = Convert.ToDateTime(date).AddDays(-1);
                     DateTime endDate = startDate.AddDays(2);
                     string newFilter = "('" + startDate.ToString("MM/dd/yyyy") + "' < [Date] AND [Date] < '" + endDate.ToString("MM/dd/yyyy") + "')";

                     GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe(columnName);
                     dateColumn.CurrentFilterValue = Convert.ToDateTime(date).ToString("MM/dd/yyyy");
                     ActivityLogGrid.MasterTableView.FilterExpression = newFilter;
                     ActivityLogGrid.Rebind();
                 }

             }

         }

But ReBind does not work.

I use:
- AutogenerateColumns = true
and NeedSource events.


regards

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 16 Feb 2012, 02:16 PM
0
July
Top achievements
Rank 2
answered on 16 Feb 2012, 02:19 PM
I saw this link, but does nto work for me!
I need SHOW DATE + Hour in Grid.
and I create the grid using autogenerateColums = true..


this is my code

 protected void ActivityLog_ItemCommand(object sender, GridCommandEventArgs e)
         {
             if (e.CommandName == RadGrid.FilterCommandName)
             {
                 Pair filterPair = e.CommandArgument as Pair;
                 string columnName = Convert.ToString(filterPair.Second);
                 if (columnName == "Date")
                 {
                     e.Canceled = true;

                     string date =
                         ((RadDatePicker)((GridFilteringItem)e.Item)[Convert.ToString(filterPair.Second)].Controls[0])
                             .SelectedDate.ToString();
                     DateTime startDate = Convert.ToDateTime(date).AddDays(-1);
                     DateTime endDate = startDate.AddDays(2);
                     string newFilter = "('" + startDate.ToString("MM/dd/yyyy") + "' < [Date] AND [Date] < '" + endDate.ToString("MM/dd/yyyy") + "')";

                     GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe(columnName);
                     dateColumn.CurrentFilterValue = Convert.ToDateTime(date).ToString("MM/dd/yyyy");
                     ActivityLogGrid.MasterTableView.FilterExpression = newFilter;
                     ActivityLogGrid.Rebind();
                 }

             }

         }

 protected void ActivityLogGrid_ItemCreated(object sender, GridItemEventArgs e)
        {
            ActivityLogGrid.MasterTableView.GetColumnSafe("RowIndicator").Display = false;
            ActivityLogGrid.MasterTableView.GetColumnSafe("Id").HeaderStyle.Width = Unit.Pixel(100);
            ActivityLogGrid.MasterTableView.GetColumnSafe("Level").HeaderStyle.Width = Unit.Pixel(120);
            ActivityLogGrid.MasterTableView.GetColumnSafe("Id").FilterControlWidth = Unit.Pixel(50);

            ActivityLogGrid.MasterTableView.GetColumnSafe("Date").AutoPostBackOnFilter = true;

        }
  protected void ActivityLogGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            LoadData();

        }

  private void LoadData()
        {

            DateTime minDate;
            DateTime maxDate;
            ActivityLogGrid.Visible = true;


            minDate = string.IsNullOrEmpty(rdpMinDate.DateInput.Text) ? Convert.ToDateTime(rdpMinDate.MinDate) : Convert.ToDateTime(rdpMinDate.SelectedDate);

            maxDate = string.IsNullOrEmpty(rdpMaxDate.DateInput.Text) ? Convert.ToDateTime(rdpMaxDate.MaxDate) : Convert.ToDateTime(rdpMaxDate.SelectedDate);

            int logType = Convert.ToInt16(radCbTypeReport.SelectedValue);
            ActivityLogGrid.DataSource = _organization.GetActivityLog(logType, minDate, maxDate);

            btnPrint.Visible = true;
            btnRemoveSelected.Visible = true;





        }


ASPX

 

              <Telerik:RadGrid ID="ActivityLogGrid" runat="server"  CssClass="ActivityGrid"
                   AllowPaging="True" AllowSorting="True" Skin="Vista" PageSize="10" OnItemCommand="ActivityLog_ItemCommand"
                  AutoGenerateColumn="true" EnableHeaderContextMenu ="true" EnableHeaderContextFilterMenu="true" EnableLinqExpressions="false"
                    GridLines="None" CellSpacing="0" AllowMultiRowSelection="true"
                    AllowFilteringByColumn="true" onitemcreated="ActivityLogGrid_ItemCreated"
                    onneeddatasource="ActivityLogGrid_NeedDataSource"  
                    onprerender="ActivityLogGrid_PreRender"   Width="100%" Height="300px" >
                     <ItemStyle VerticalAlign="Top"   />
           
            <GroupingSettings CaseSensitive="false" />
         
            <MasterTableView  DataKeyNames="Id"  CommandItemDisplay="None" AllowFilteringByColumn="true"
                TableLayout="Fixed"  PagerStyle-Mode="NumericPages" Width="100%" EnableHeaderContextMenu="true" >
                 <Columns>
                        <telerik:GridClientSelectColumn   CommandName="Select" UniqueName="Select">
                        <HeaderStyle Width="33px" />  
                        <ItemStyle  Width="33px" />  
                        </telerik:GridClientSelectColumn>     
                         
                   
                                   
                    </Columns>
                <ExpandCollapseColumn Visible="False">
                    <HeaderStyle Width="19px" />
                </ExpandCollapseColumn>
                <RowIndicatorColumn Visible="False">
                    <HeaderStyle Width="20px" />
                </RowIndicatorColumn>
                </MasterTableView>
                 <ClientSettings AllowRowsDragDrop="true"    ActiveRowIndex="true" AllowColumnsReorder="true" AllowColumnHide="true">
                
                           <Resizing AllowColumnResize="true" ClipCellContentOnResize="true" EnableRealTimeResize="true" ></Resizing>
            <Selecting AllowRowSelect="true" />
                    <Scrolling AllowScroll="true" UseStaticHeaders="true"  />
                     <ClientEvents OnColumnHidden="onColumnHidden" />
                </ClientSettings>
                <PagerStyle  Mode="NumericPages"/>

               </Telerik:RadGrid>

0
Accepted
Tsvetina
Telerik team
answered on 21 Feb 2012, 07:49 AM
Hello July,

Have you tried the filter query that you are manually building as a direct query to your database to make sure that it returns records? This would be the first step in debugging the scenario.

Also I see that you once add a filter expression but then in NeedDataSource you again perform some filtering on the data, so can you confirm that the two filters do not have common records?

Regards,
Tsvetina
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
July
Top achievements
Rank 2
answered on 24 Feb 2012, 02:27 PM
I find that error is on Date format.
RadGrid culture is en-US
and in SP I use es-AR

Thanks!

Tags
Grid
Asked by
July
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
July
Top achievements
Rank 2
Tsvetina
Telerik team
Share this question
or