简体   繁体   中英

Filter RadGrid data, based on YEAR and MONTH DropDownList from Date column on button click

I require that when a page load, all the records in a SQL-database shall be displayed in a Telerik RadGrid.

  • If a user select any YEAR alone from Dropdown and click on button, then all the records shall filter based on selected year using "Submitted Date" column. (ie, filter shall affect Submitted Date column only)
  • If user select any MONTH alone from Dropdown and click on button, then all the records shall filter based on selected month using "Submitted Date" column. (ie, filter shall affect Submitted Date column only)
  • If a user select any YEAR and MONTH together from Dropdown and click on button, then all the records shall filter based on selected year & month using "Submitted Date" column. (ie, filter shall affect Submitted Date column only)

How would I write a Stored Procedure for this?

Also, do I have to write 2 different Stored Procedures? One when no filter is applied and I need to show all the records on page load, and another when filter is applied and I have to show all the records based on filter?

Below is the Stored Procedure I am using to bind the RadGrid.

ALTER PROCEDURE [Invoice].[usp_tbl_Request_Select_MonthlyStatusReport]
    -- Add the parameters for the stored procedure here

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Select statements for procedure here

 Select * FROM
 (
 SELECT distinct
 R.[RequestID],
 R.[RequisitionNo],
 R.[BarcodeNo],
 R.[IsThreeWayMatch],
 R.[Company],
 R.[TotalInvoiceAmount],
 R.[Status],

 X.[SupplierName],
 X.ScanOwner as ScanName,
 X.ScanTime, --Show date, time in diff column in Grid
 UPPER(X.VerifyOwner) as VerifyOwner,
 X.VerifyTime, --Show date, time in diff column in Grid

 W.Action, 
 W.UserName as SubmitterName,
 W.CreatedDate AS  SubmitterDateTime--Show date, time in diff column in Grid

 From [Invoice].[tbl_Request] (NOLOCK) R
 Left Join [Invoice].[tbl_Xml](NOLOCK) X On X.XmlID = R.XmlID
 Left Join [Invoice].[tbl_WorkflowHistory] (NOLOCK) W On W.RequestID = R.RequestID

 Where Action = 'Submit') as A

 Join

(SELECT RequestID, Action, UserName as UpdateName , CreatedDate AS  UpdateDateTime
 FROM [Sunway_AP].[Invoice].[tbl_WorkflowHistory]
 Where Action = 'Update Additional Info') as B 

 ON A.RequestID=B.RequestID

END

There are 2 DropDownList and 1 button, inside WebPage.

  1. MONTH dropdownList, in which month is hard code from January to December.
  2. YEAR dropdownList, in which year is hard code from 2000 to 2030.

In RadGrid there are 4 date columns I am showing:

  1. Scan Date
  2. Verify Date
  3. Submitted Date
  4. Updated Date

Below is the RadGrid HTML code :

<telerik:RadGrid ID="GridReport" runat="server" AllowPaging="True" AllowSorting="True" AllowFilteringByColumn="True" ShowGroupPanel="True"
        CellSpacing="0" GridLines="None" Width="100%" OnNeedDataSource="GridReport_NeedDataSource" OnItemDataBound="GridReport_ItemDataBound">

                        <ClientSettings AllowDragToGroup="True" />
                        <GroupingSettings CaseSensitive="false"></GroupingSettings>
                            <MasterTableView AllowFilteringByColumn="true" AllowMultiColumnSorting="false" AutoGenerateColumns="false"
                                    CommandItemDisplay="Top" DataKeyNames="RequestID" EnableGroupsExpandAll="true"
                                    GroupLoadMode="Client" PageSize="50">

                            <CommandItemSettings ShowAddNewRecordButton="false" ShowExportToExcelButton="true" />
                            <SortExpressions>
                                <telerik:GridSortExpression FieldName="RequisitionNo" SortOrder="Descending" />
                            </SortExpressions>
                            <PagerStyle AlwaysVisible="True" PageSizeControlType="RadComboBox" Position="Bottom" PageSizes="50,100,150,200" />

                                    <Columns>

                                        <telerik:GridBoundColumn DataField="RequestID" Visible="false" HeaderText="Request ID" SortExpression="RequestID"
                                            UniqueName="RequestID" FilterDelay="2000" ShowFilterIcon="false" FilterControlWidth="150px">
                                        </telerik:GridBoundColumn>

                                        <telerik:GridBoundColumn DataField="ScanTime" HeaderText="Scan Date" SortExpression="ScanTime"
                                            UniqueName="ScanDate" FilterDelay="2000" ShowFilterIcon="false" dataformatstring="{0:MM/dd/yyyy}">
                                        </telerik:GridBoundColumn>

                                        <telerik:GridBoundColumn DataField="ScanTime" HeaderText="Scan Time" SortExpression="ScanTime"
                                            UniqueName="ScanTime" FilterDelay="2000" ShowFilterIcon="false" DataFormatString="{0:HH:mm:ss tt}">
                                        </telerik:GridBoundColumn>

                                        <telerik:GridBoundColumn DataField="VerifyTime" HeaderText="Verifier Date" SortExpression="VerifyTime"
                                            UniqueName="VerifyDate" FilterDelay="2000" ShowFilterIcon="false" dataformatstring="{0:MM/dd/yyyy}">
                                        </telerik:GridBoundColumn>

                                        <telerik:GridBoundColumn DataField="VerifyTime" HeaderText="Verifier Time" SortExpression="VerifyTime"
                                            UniqueName="VerifyTime" FilterDelay="2000" ShowFilterIcon="false" DataFormatString="{0:HH:mm:ss tt}">
                                        </telerik:GridBoundColumn>

                                         <telerik:GridBoundColumn DataField="SubmitterDateTime" HeaderText="Submitter Date" SortExpression="SubmitterDateTime"
                                            UniqueName="SubmitterDate" FilterDelay="2000" ShowFilterIcon="false" dataformatstring="{0:MM/dd/yyyy}">
                                        </telerik:GridBoundColumn>

                                        <telerik:GridBoundColumn DataField="SubmitterDateTime" HeaderText="Submitter Time" SortExpression="SubmitterDateTime"
                                            UniqueName="SubmitterTime" FilterDelay="2000" ShowFilterIcon="false" DataFormatString="{0:HH:mm:ss tt}">
                                        </telerik:GridBoundColumn>

                                        <telerik:GridBoundColumn DataField="UpdateDateTime" HeaderText="Updated Date" SortExpression="UpdateDateTime"
                                            UniqueName="UpdateDate" FilterDelay="2000" ShowFilterIcon="false" HeaderStyle-Width="400%" dataformatstring="{0:MM/dd/yyyy}">
                                        </telerik:GridBoundColumn>

                                        <telerik:GridBoundColumn DataField="UpdateDateTime" HeaderText="Updated Time" SortExpression="UpdateDateTime"
                                            UniqueName="UpdateTime" FilterDelay="2000" ShowFilterIcon="false" DataFormatString="{0:HH:mm:ss tt}">
                                        </telerik:GridBoundColumn>

                                    </Columns>
                                </MasterTableView>

                                <ExportSettings SuppressColumnDataFormatStrings="True" IgnorePaging="True" ExportOnlyData="True" Excel-Format="ExcelML" OpenInNewWindow="True" FileName="eAPDocHistory" Excel-FileExtension="xls">
                                </ExportSettings>
                            </telerik:RadGrid>

Please reply. Thanks in advance

Below Stored Procedure is working fine based on above requirement:

ALTER PROCEDURE [Invoice].[usp_tbl_Request_Select_MonthlyStatusReport]
    -- Add the parameters for the stored procedure here
      @Month VARCHAR(10),
      @Year VARCHAR(10)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Select statements for procedure here

IF (@Month <> 0 And @Year = 0)
BEGIN

Select * FROM
 (
 SELECT distinct
 R.[RequestID],
 R.[RequisitionNo],
 R.[BarcodeNo],
 R.[IsThreeWayMatch],
 R.[Company],
 R.[TotalInvoiceAmount],
 R.[Status],

 X.[SupplierName],
 X.ScanOwner as ScanName,
 X.ScanTime, --Show date, time in diff column in Grid
 UPPER(X.VerifyOwner) as VerifyOwner,
 X.VerifyTime, --Show date, time in diff column in Grid

 W.Action, 
 W.UserName as SubmitterName,
 W.CreatedDate AS SubmitterDateTime--Show date, time in diff column in Grid

 From [Invoice].[tbl_Request] (NOLOCK) R
 Left Join [Invoice].[tbl_Xml](NOLOCK) X On X.XmlID = R.XmlID
 Left Join [Invoice].[tbl_WorkflowHistory] (NOLOCK) W On W.RequestID = R.RequestID

 Where Action = 'Submit' AND DATEPART(MONTH,W.CreatedDate) = @Month) as A

 Join

(SELECT RequestID, Action, UserName as UpdateName , CreatedDate AS  UpdateDateTime
 FROM [Sunway_AP].[Invoice].[tbl_WorkflowHistory]
 Where Action = 'Update Additional Info' AND DATEPART(MONTH,CreatedDate) = @Month) as B 

 ON A.RequestID=B.RequestID

END

Else IF (@Year <> 0 And @Month = 0)
BEGIN

Select * FROM
 (
 SELECT distinct
 R.[RequestID],
 R.[RequisitionNo],
 R.[BarcodeNo],
 R.[IsThreeWayMatch],
 R.[Company],
 R.[TotalInvoiceAmount],
 R.[Status],

 X.[SupplierName],
 X.ScanOwner as ScanName,
 X.ScanTime, --Show date, time in diff column in Grid
 UPPER(X.VerifyOwner) as VerifyOwner,
 X.VerifyTime, --Show date, time in diff column in Grid

 W.Action, 
 W.UserName as SubmitterName,
 W.CreatedDate AS SubmitterDateTime--Show date, time in diff column in Grid

 From [Invoice].[tbl_Request] (NOLOCK) R
 Left Join [Invoice].[tbl_Xml](NOLOCK) X On X.XmlID = R.XmlID
 Left Join [Invoice].[tbl_WorkflowHistory] (NOLOCK) W On W.RequestID = R.RequestID

 Where Action = 'Submit' AND DATEPART(YEAR,W.CreatedDate) = @Year) as A

 Join

(SELECT RequestID, Action, UserName as UpdateName , CreatedDate AS  UpdateDateTime
 FROM [Sunway_AP].[Invoice].[tbl_WorkflowHistory]
 Where Action = 'Update Additional Info' AND DATEPART(YEAR,CreatedDate) = @Year) as B 

 ON A.RequestID=B.RequestID

END

IF (@Month <> 0 And @Year <> 0)
BEGIN

Select * FROM
 (
 SELECT distinct
 R.[RequestID],
 R.[RequisitionNo],
 R.[BarcodeNo],
 R.[IsThreeWayMatch],
 R.[Company],
 R.[TotalInvoiceAmount],
 R.[Status],

 X.[SupplierName],
 X.ScanOwner as ScanName,
 X.ScanTime, --Show date, time in diff column in Grid
 UPPER(X.VerifyOwner) as VerifyOwner,
 X.VerifyTime, --Show date, time in diff column in Grid

 W.Action, 
 W.UserName as SubmitterName,
 W.CreatedDate AS  SubmitterDateTime--Show date, time in diff column in Grid

 From [Invoice].[tbl_Request] (NOLOCK) R
 Left Join [Invoice].[tbl_Xml](NOLOCK) X On X.XmlID = R.XmlID
 Left Join [Invoice].[tbl_WorkflowHistory] (NOLOCK) W On W.RequestID = R.RequestID

 Where Action = 'Submit' AND DATEPART(YEAR,W.CreatedDate) = @Year AND DATEPART(MONTH,W.CreatedDate) = @Month) as A

 Join

(SELECT RequestID, Action, UserName as UpdateName , CreatedDate AS  UpdateDateTime
 FROM [Sunway_AP].[Invoice].[tbl_WorkflowHistory]
 Where Action = 'Update Additional Info' AND DATEPART(YEAR,CreatedDate) = @Year AND DATEPART(MONTH,CreatedDate) = @Month) as B 

 ON A.RequestID=B.RequestID

END

ELSE
BEGIN

Select * FROM
 (
 SELECT distinct
 R.[RequestID],
 R.[RequisitionNo],
 R.[BarcodeNo],
 R.[IsThreeWayMatch],
 R.[Company],
 R.[TotalInvoiceAmount],
 R.[Status],

 X.[SupplierName],
 X.ScanOwner as ScanName,
 X.ScanTime, --Show date, time in diff column in Grid
 UPPER(X.VerifyOwner) as VerifyOwner,
 X.VerifyTime, --Show date, time in diff column in Grid

 W.Action, 
 W.UserName as SubmitterName,
 W.CreatedDate AS  SubmitterDateTime--Show date, time in diff column in Grid

 From [Invoice].[tbl_Request] (NOLOCK) R
 Left Join [Invoice].[tbl_Xml](NOLOCK) X On X.XmlID = R.XmlID
 Left Join [Invoice].[tbl_WorkflowHistory] (NOLOCK) W On W.RequestID = R.RequestID

 Where Action = 'Submit') as A

 Join

(SELECT RequestID, Action, UserName as UpdateName , CreatedDate AS  UpdateDateTime
 FROM [Sunway_AP].[Invoice].[tbl_WorkflowHistory]
 Where Action = 'Update Additional Info') as B 

 ON A.RequestID=B.RequestID

END

END

I recommend the following:

  1. In the input parameters adds: @Date DATETIME = NULL;

  2. In the Where adds: AND YEAR(W.CreatedDate) = COALESCE(@Date, YEAR(W.CreatedDate)) as A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM