将程序数据集分配给报告

时间:2014-07-15 12:00:39

标签: c# asp.net gridview

我已经使用gridview中的行填充了数据集。现在我试图将其分配给RDLC报告,但我不知道如何。它必须需要.xsd,但我只需要分配程序化数据集。

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

    <div class="widget">
        <div class="widget-header">
                <div class="title">
                    Report- Complaints By Closing Date
                    <span id="sectionDetails" class="mini-title" runat="server"> </span>
                </div> <!-- widget title-->
        </div> <!-- widget header -->

        <div class="widget-body">
            <div class="row-fluid">
                <!-- cons start -->
                <div class="shortcut-group">
                    <asp:HiddenField ID="HiddenFieldSetMessage" runat="server" ClientIDMode="Static" />
                    <asp:HiddenField ID="HiddenFieldShowMessage" runat="server" ClientIDMode="Static" />
                    <asp:HiddenField ID="HiddenFieldShowHideButtons" runat="server" ClientIDMode="Static" />
                    &nbsp;&nbsp; Date From:
                    <asp:TextBox ID="txtDateFrom" runat="server" ></asp:TextBox>
                    &nbsp;&nbsp; Date To:
                    <asp:TextBox ID="txtDateTo" runat="server"></asp:TextBox>
                    <asp:CalendarExtender ID="CalendarExtenderDateFrom"  Format="dd/MMM/yyyy" TargetControlID="txtDateFrom" runat="server"></asp:CalendarExtender>
                    <asp:CalendarExtender ID="CalendarExtenderDateTo"  Format="dd/MMM/yyyy" TargetControlID="txtDateTo" runat="server"></asp:CalendarExtender>
                    &nbsp;
                    <span id="Save-Span">
                        <asp:Button ID="btnSearch"  CssClass="btn btn-success" runat="server" Text="Search" ValidationGroup="ValidationGroupTextboxes" ClientIDMode="Static" OnClick="btnSearch_Click" />
                        <br />
                        <asp:GridView ID="GridViewSmsComplaints" AllowPaging="False" runat="server" OnPageIndexChanging="GridViewSmsComplaints_PageIndexChanging" AutoGenerateColumns="False" CssClass="mGrid" BorderColor="#333333"  Width="550px">
                            <Columns>
                                <asp:BoundField HeaderText="ID" DataField="ID" /> 
                                <asp:BoundField HeaderText="Recieving Date" DataField="RecievingDate" />  
                                <asp:BoundField HeaderText="FromMobileNo" DataField="FromMobileNo" /> 
                                <asp:BoundField HeaderText="Message" DataField="Message">   
                                    <ItemStyle Wrap="True" />
                                </asp:BoundField>
                                <asp:TemplateField HeaderText="IsComplaint">
                                    <ItemTemplate>
                                        <asp:DropDownList ID="ddlValidity" runat="server">
                                            <asp:ListItem Value="1" Text="Valid"></asp:ListItem>
                                            <asp:ListItem Value="0" Text="Invalid"></asp:ListItem>
                                        </asp:DropDownList>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                        <br />
                    </span>  
                    <asp:Button ID="btnGenerateReport"  CssClass="btn btn-success" runat="server" Text="Search" ValidationGroup="ValidationGroupTextboxes" ClientIDMode="Static" OnClick="btnGenerateReport_Click" />
                    <br />
                    <br />
                    <br />
                    <rsweb:ReportViewer ID="ReportViewer1" runat="server">
                    </rsweb:ReportViewer>
                </div> <!-- shourtcut group">
                <!-- cons end -->
            </div> <!-- row fluid -->
        </div> <!-- widget body-->
    </div>
</asp:Content>

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        txtDateFrom.Text = DateTime.Now.ToString("dd/MMM/yyyy");
        txtDateTo.Text = DateTime.Now.ToString("dd/MMM/yyyy");

        ReportViewer1.AsyncRendering = false;
        ReportViewer1.SizeToReportContent = true;
        ReportViewer1.ZoomMode = ZoomMode.FullPage;
        this.ReportViewer1.Reset();
    }
}

protected void btnGenerateReport_Click(object sender, EventArgs e)
{
    dtRoom.Columns.Add(new DataColumn("ID", typeof(string)));
    dtRoom.Columns.Add(new DataColumn("RecievingDate", typeof(string)));
    dtRoom.Columns.Add(new DataColumn("FromMobileNo", typeof(string)));
    dtRoom.Columns.Add(new DataColumn("Message", typeof(string)));
    dtRoom.Columns.Add(new DataColumn("IsComplaint", typeof(bool)));
    bool var = false;
    for (int i = 0; i <= GridViewSmsComplaints.Rows.Count; i++)
    {
        DropDownList IsValid = (DropDownList) GridViewSmsComplaints.Rows[i].FindControl("ddlValidity");
        if (IsValid.SelectedValue == "1")
        {
            var = true;
            DataRow datarw = null;
            //dtRoom = new DataTable();
            datarw = dtRoom.NewRow();

            datarw[0] = GridViewSmsComplaints.Rows[i].Cells[0].Text;
            datarw[1] = GridViewSmsComplaints.Rows[i].Cells[1].Text;
            datarw[2] = GridViewSmsComplaints.Rows[i].Cells[2].Text;
            datarw[3] = GridViewSmsComplaints.Rows[i].Cells[3].Text;
            datarw[4] = var; // Convert.ToBoolean(GridViewSmsComplaints.Rows[i].Cells[4].Text);

            dtRoom.Rows.Add(datarw);
        }
    }
    GridViewSmsComplaints.DataSource = dtRoom;
    GridViewSmsComplaints.DataBind();
    Response.Write("Done");

    if (dtRoom.Rows.Count <= 0)
    {
        HiddenFieldSetMessage.Value = "NotExists";
        HiddenFieldShowMessage.Value = "True";
        ReportViewer1.Visible = false;
    }
    else
    {
        ReportDataSource rpds = new ReportDataSource("DataSet1", dtRoom);
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(rpds);
        ReportViewer1.Visible = true;
    }
}

我不知道也无法想办法怎么做?

2 个答案:

答案 0 :(得分:0)

在报告绑定代码中,您可以执行以下操作将数据表分配给报告

ReportDataSource reportDataSource = new ReportDataSource();
IList<string> names = ReportViewer1.LocalReport.GetDataSourceNames();
reportDataSource.Name = names[0];
reportDataSource.Value = rpds.Tables[0];
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(reportDataSource);
ReportViewer1.Visible = true;

答案 1 :(得分:0)

这超出了您的需要..但请检查它,因为它可能是所有报告动态报告的最佳解决方案。

您可以构建动态创建RDLC和数据集的报告

为此,请从http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition/ReportDefinition.xsd 获取XSD报告定义,并使用XML架构定义工具xsd.exe将其转换为Class。

xsd file.xsd {/classes | /dataset} [/element:element]
         [/language:language] [/namespace:namespace]
         [/outputdir:directory] [URI:uri]

然后你需要一个ReportGenerator,它使用ReportDefinition.cs来创建报告和对象。

http://www.gotreportviewer.com/ 动态生成RDLC有一个很好的例子 - 矩阵

我的意思是,您可以构建自己的RDLC(RDLC文件只是一个XML文件)并为您的目的更改任何内容(我用它来绘制我的徽标公司,添加页脚等,而不制作任何RDCL文件使用Visual Studio)。