开发者

How to show crystal report from GridView?

I have a GridView with one ButtonField column like this: I h开发者_如何学Goave already Created Crystal Report based On stored procedure with one input Parameter called ID. I kept the ID in DataKeyNames of the GridView.

So when the user click the print button in the Gridview, the corresponding crystal reports to be displayed.

Eagerly waiting for your useful tips.


There is no direct way to export gridview to crystal report.

Call the same query using dataset, pass dataset to report.


I got the answer.

        if (e.CommandName == "printReport")      
        {
            int ID = Convert.ToInt32(GridView1.SelectedDataKey.Value);
            try
            {
                CrystalDecisions.CrystalReports.Engine.ReportDocument rpt =
                            new CrystalDecisions.CrystalReports.Engine.ReportDocument();

                string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
                string[] str = conn.Split(';');
                string server = str[0].Substring(str[0].IndexOf(" = ") + 3);
                string database = str[1].Substring(str[1].IndexOf(" = ") + 3);
                string userid = str[2].Substring(str[2].IndexOf(" = ") + 3);
                string password = str[3].Substring(str[3].IndexOf(" = ") + 3);

                rpt.Load(Server.MapPath("~/Engineering/Reports/Report.rpt"));

                for (int i = 0; i < rpt.DataSourceConnections.Count; i++)
                    rpt.DataSourceConnections[i].SetConnection(server, database, userid, password);
                rpt.SetParameterValue(0, ID);

                rpt.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, HttpContext.Current.Response, true, "Report");
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }

        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜