如何将数组传递给rdlc报告?

时间:2014-02-10 07:28:03

标签: asp.net vb.net crystal-reports parameter-passing rdlc

我提供了一个radcombobox,让用户选择一个或多个部门来生成报告(由asp.net报告(RDLC)开发)。

我开发了以下数据集:

SELECT     c.DeptID, c.Department, b.Course_Name, b.Course_ID, b.Type, b.Ref_Code, b.Exam
FROM         dbo.db_Competency_List AS a INNER JOIN
                      dbo.db_Course AS b ON a.Course_ID = b.Course_ID INNER JOIN
                      dbo.db_Department AS c ON a.Dept_ID = c.DeptID
where a.Dept_ID in (@Para_DID)

enter image description here

enter image description here

如果用户仅选择一个部门,则可以使用。

但如果用户选择多个部门,则数据集似乎无法获取参数。 例如12,33,65,78 ...

代码:

    <telerik:RadComboBox ID="rcb_select_dept" runat="server"  DataSourceID="LDS_ddl_dept" DataTextField="Department" AutoPostBack="True" 
           DefaultMessage="Please Select" DataValueField="DeptID" Width="300" CheckBoxes="true" 
           AllowCustomText="true" >
    </telerik:RadComboBox>
       <asp:LinqDataSource ID="LDS_ddl_dept" runat="server" 
           ContextTypeName="dcLRDBDataContext" EntityTypeName="" TableName="db_Departments" OrderBy="Department">
       </asp:LinqDataSource>
       </td>
   </tr>
   </table>

   <br />

   <rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="1200px" 
                       Height="800px" Font-Names="Verdana" Font-Size="8pt" 
                       InteractiveDeviceInfos="(Collection)" WaitMessageFont-Names="Verdana" 
                       WaitMessageFont-Size="14pt">
                       <LocalReport ReportPath="Reports\template\RequiredByDepartment.rdlc">
                         <DataSources>
                              <rsweb:ReportDataSource DataSourceId="ObjectDataSource1" 
                                    Name="DS_Content" />           
                              </DataSources>
                       </LocalReport>
                   </rsweb:ReportViewer>
<br />

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" 
    TypeName="LRDBDataSetTableAdapters.vReqByDeptTableAdapter">
    <SelectParameters>
                <asp:Parameter DefaultValue="0" Name="Para_DID" Type="String" />                        
    </SelectParameters>
</asp:ObjectDataSource>

代码背后:

Protected Sub rtbMenu_ButtonClick(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadToolBarEventArgs) Handles rtbMenu.ButtonClick
    If e.Item.Value = "Generate" Then

        'Get DID
        Dim strCOM_cmb As String = ""

        For i = 0 To rcb_select_dept.CheckedItems.Count - 1
            If rcb_select_dept.CheckedItems(i).Checked = True Then
                strCOM_cmb += rcb_select_dept.CheckedItems(i).Value.ToString & ","
            End If
        Next
        If Left(strCOM_cmb, 1) = "," Then
            strCOM_cmb = Right(strCOM_cmb, Len(strCOM_cmb) - 1)
        End If
        If Right(strCOM_cmb, 1) = "," Then
            strCOM_cmb = Left(strCOM_cmb, Len(strCOM_cmb) - 1)
        End If
        strCOM_cmb = TrimList(strCOM_cmb)

        Dim params(0) As Microsoft.Reporting.WebForms.ReportParameter
        params(0) = New Microsoft.Reporting.WebForms.ReportParameter("Para_DID", strCOM_cmb)
        ReportViewer1.LocalReport.SetParameters(params)

        ObjectDataSource1.SelectParameters("Para_DID").DefaultValue = strCOM_cmb
        ObjectDataSource1.DataBind()

        Preview()
    End If
End Sub

Sub Preview()

    ReportViewer1.Visible = True
    ReportViewer1.LocalReport.Refresh()

End Sub

如何将部门ID数组(参数)传递给报告?

感谢。

3 个答案:

答案 0 :(得分:0)

解决此问题的最佳方法是将值数组作为逗号分隔的字符串(或者您希望的其他分隔符)传递,然后在报告中将其拆分。据我所知,没有一种简单的方法可以将多个值作为单个参数传递给报表。

答案 1 :(得分:0)

我建议你在SQL实现中处理这个问题。只是可选的。

要传递文本参数并希望它作为列表工作,您应该在表中表示它们,然后将查询结果加入表中的该列表。请参阅以下代码:

创建新的表变量并将列表中的每个成员插入表中:

DECLARE @SelectedDeptIds table
(
    DeptID int
)

DECLARE @deptIdTemp varchar(10), @Pos int

SET @Para_DID = LTRIM(RTRIM(@Para_DID))+ ','
SET @Pos = CHARINDEX(',', @Para_DID, 1)

IF REPLACE(@Para_DID, ',', '') <> ''
BEGIN
        WHILE @Pos > 0
    BEGIN
        SET @deptIdTemp = LTRIM(RTRIM(LEFT(@Para_DID, @Pos - 1)))
        IF @deptIdTemp <> ''
        BEGIN
            INSERT INTO @SelectedDeptIds (DeptID) VALUES (CAST(@deptIdTemp AS int))
        END
        SET @Para_DID = RIGHT(@Para_DID, LEN(@Para_DID) - @Pos)
        SET @Pos = CHARINDEX(',', @Para_DID, 1)
    END
END 

下一个代码是将表连接到列表:

SELECT     c.DeptID, c.Department, b.Course_Name, b.Course_ID, b.Type, b.Ref_Code, b.Exam
FROM       dbo.db_Competency_List AS a INNER JOIN
           dbo.db_Course AS b ON a.Course_ID = b.Course_ID INNER JOIN
           dbo.db_Department AS c ON a.Dept_ID = c.DeptID
INNER JOIN @SelectedDeptIds d ON a.Dept_ID = d.DeptID 

答案 2 :(得分:0)

我创建了一个tmp表。

首先删除现有的DeptID(UserID =生成报告的人) 然后将选定的DeptID和UserID插入到后面代码中的tmp表中。

然后修改数据集:

SELECT     c.DeptID, c.Department, b.Course_Name, b.Course_ID, b.Type, b.Ref_Code, b.Exam, a.RoleType_ID
FROM         dbo.db_Competency_List AS a INNER JOIN
                      dbo.db_Course AS b ON a.Course_ID = b.Course_ID INNER JOIN
                      dbo.db_Department AS c ON a.Dept_ID = c.DeptID
where a.Dept_ID in (SELECT id from dbo.db_tmp_rpt where UserID=@UserID)