2010-02-15 28 views
5

Làm cách nào để lập trình nguồn dữ liệu cho điều khiển ASP.NET ReportViewer?Làm cách nào để lập trình nguồn dữ liệu cho điều khiển ASP.NET ReportViewer?

Tôi có điều khiển VS ReportViewer 2008 và muốn chuyển đổi giữa một vài báo cáo khác nhau.

Tôi có thể chuyển báo cáo bằng cách đặt nguồn báo cáo và làm mới kiểm soát, nhưng tôi không thể biết vị trí đặt nguồn dữ liệu.

Mỗi báo cáo có nguồn dữ liệu riêng và nếu tôi định cấu hình ban đầu khi điều khiển được xây dựng thì tốt, nhưng tôi cần chuyển đổi giữa chúng.

+0

Bạn đang nói chuyện về điều khiển ReportViewer hoặc một số điều khiển khác? – womp

+0

Vâng, tôi cảm ơn. – Maestro1024

Trả lời

4

Tôi giả định câu hỏi là về kiểm soát ReportViewer.

reportViewer.LocalReport.DataSources.Clear(); 
reportViewer.LocalReport.DataSources.Add(new ReportDataSource("dsname", source)); 

"dsname" là tên của nguồn dữ liệu, bạn có thể tìm thấy tệp .rdlc. source là biến có dữ liệu bạn muốn hiển thị trong báo cáo.

+0

Khi II thực hiện việc này, tôi nhận được "# Đã xảy ra lỗi trong quá trình xử lý báo cáo. * Trường hợp nguồn dữ liệu chưa được cung cấp cho nguồn dữ liệu ...." – Maestro1024

+0

Và nguồn dữ liệu được cung cấp được liệt kê trong nguồn dữ liệu cho báo cáo mặc định mà ban đầu tôi đã thiết lập. Nó giống như nguồn dữ liệu mới không thực sự được thiết lập. – Maestro1024

+0

Tôi vẫn đang xem xét điều này. Tự hỏi nếu tôi có sai "tên nguồn". Làm cách nào để xem nguồn được sử dụng trong báo cáo? – Maestro1024

1

1) đánh dấu Basic:

<asp:ScriptManager ID="ScriptManager1" runat="server" /> 
<rsweb:ReportViewer ID="rptView" Width="1000px" ProcessingMode="Local" 
     Font-Names="Verdana" Font-Size="8pt" InteractiveDeviceInfos="(Collection)" 
     WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" runat="server" > 
</rsweb:ReportViewer> 

2) Chỉnh sửa XML báo cáo. Thiết lập bộ dữ liệu & tên trường của bạn:

<DataSets> 
    <DataSet Name="dsSource"> 
    <Fields> 
     <Field Name="MyField1"> 
     <DataField>MyField1</DataField> 
     <rd:TypeName>System.String</rd:TypeName> 
     </Field> 
    <Query> 
     <DataSourceName>dsSource</DataSourceName> 
     <CommandText>/* Local Query */</CommandText> 
    </Query> 
    </DataSet> 
</DataSets> 

3) Thiết lập nguồn dữ liệu trên cả hai báo cáo & xem báo cáo (không biết tại sao ... cả hai đều cần thiết)

SqlConnection cn = new SqlConnection(_connectionString); 
SqlCommand cmd = new SqlCommand("dbo.MyProc", cn); 
cmd.CommandType = CommandType.StoredProcedure; 
SqlDataAdapter da = new SqlDataAdapter(cmd); 
DataTable tbl = new DataTable(); 

cn.Open(); 
da.Fill(tbl); 
cn.Close(); 

rptView.Visible = true; 
rptView.LocalReport.DataSources.Clear(); 
ReportDataSource rptData = new ReportDataSource("dsSource", tbl); 

LocalReport r = new LocalReport(); 
r.ReportPath = Server.MapPath("~/Reports/MyReport.rdlc"); 
r.DataSources.Add(rptData); 

rptView.LocalReport.DataSources.Add(rptData); 
rptView.LocalReport.ReportPath = Server.MapPath("~/Reports/MyReport.rdlc"); 
rptView.LocalReport.Refresh(); 
0
GlobalReportViewer.AsyncRendering = True 
    If Session.Count > 0 Then 
     For i As Integer = 0 To Session.Count - 1 
      If Session(i).GetType().ToString() = "Microsoft.Reporting.WebForms.ReportHierarchy" Then 
       Session.RemoveAt(i) 
      End If 
     Next 
    End If 
    GlobalReportViewer.LocalReport.ReportPath = "" 
GlobalReportViewer.LocalReport.ReleaseSandboxAppDomain() 
GlobalReportViewer.LocalReport.DataSources.Add(New ReportDataSource("XXXDataSet_YYYTable", "ObjectDSZZZ")) 
GlobalReportViewer.LocalReport.ReportPath = "Reports\AAAReport.rdlc" 
GlobalReportViewer.LocalReport.Refresh() 
Các vấn đề liên quan