[C#][WebForm]Gridview to CrystalReport

  • 12726
  • 0
  • C#
  • 2009-12-24

[C#][WebForm]Gridview to CrystalReport

小舖網友問題,自己簡單實做記錄一下。

 

建立報表和設計(dataset已建立OK)

image image

image 

Default.aspx

<form id="form1" runat="server">   
    
        請輸入City:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="ShowReport" onclick="Button1_Click" />
        <br />
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>    
    
    </form>

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
        {           
            //資料庫連線
            SqlDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString"].ConnectionString;
            if(!Page.IsPostBack)
            {
                SqlDataSource1.SelectCommand = "select top 20 * from Person.Address";
                SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                GridView1.DataSourceID = SqlDataSource1.ID;
            }                       
        }
        
        #region 顯示報表
        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            String openwin = "";
            String sql = TextBox1.Text.Trim();
            if (String.IsNullOrEmpty(sql))
            {
                SqlDataSource1.SelectCommand = "select top 20 * from Person.Address";                
                dt = ((DataView)this.SqlDataSource1.Select(DataSourceSelectArguments.Empty)).ToTable();
            }
            else
            {               
                SqlDataSource1.SelectCommand = "select top 20 * from Person.Address t where t.City='" + sql + "'";
                dt = ((DataView)this.SqlDataSource1.Select(DataSourceSelectArguments.Empty)).ToTable();
            }
            Session["data"]=dt;
            //開啟新視窗
            openwin = "javascript:window.showModalDialog('ViewCR.aspx','_blank','status: on;center: yes;dialogHeight: 600px;dialogWidth: 800px;');";
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "open", openwin, true);
        }
        #endregion

       

ViewCR.aspx

 

 <form id="form1" runat="server">    
    
        <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
            AutoDataBind="true" DisplayGroupTree="False" 
            EnableDatabaseLogonPrompt="False" EnableParameterPrompt="False" 
            HasCrystalLogo="False" />    
    
    </form>

ViewCR.aspx.cs

 

protected void Page_Load(object sender, EventArgs e)
        {  
          Response.Cache.SetCacheability(HttpCacheability.NoCache); //clear cache  
            DataTable dt = new DataTable();
            dt = Session["data"] as DataTable;
            ReportDocument rd=new ReportDocument();          
            rd.Load(Server.MapPath("CrystalReport1.rpt"));
            rd.SetDataSource(dt);
            CrystalReportViewer1.ReportSource = rd;           
        }

結果(擷取部分)

image 

image