Bài 5: Tạo dynamic reports trong C#

Bài 5: Tạo dynamic reports trong C#

Mấy bài trước, chúng ta tạo report từ những database đã được chọn sẵn, bây giờ ta tạo report từ kết quả của các query SQL lúc runtime.

Ta cũng tạo project winform và cũng thêm vào 1 DataSet như bài trên kia nhưng ko kết nối vào database nào cả. Trên Dataset, ta cũng thêm 1 datatable vào DataSet đó, trên Datatable ta tạo mấy cái column như sau:

Bài 5: Tạo dynamic reports trong C#

Tạo thêm 1 cái crytal report như baì trước!
Các bạn thực hiện như các bước ở các bài trên tớ làm, cũng sử dụng ProjectData và chọn tất cả cac column của cái Datatable vừa tạo nhá.

Bi giờ ta sẽ thiết kế 1 cài Form như bên giưới,report sẽ phụ thuộc vào cac queyry lúc runtime:

Viết 1 hàm xử lý cho các query nhập vào :

public string procesSQL()
        {
            string sql = null;
            string inSql = null;
            string firstPart = null;
            string lastPart = null;
            int selectStart = 0;
            int fromStart = 0;
            string[] fields = null;
            string[] sep = { "," };
            int i = 0;
            TextObject MyText ;

            inSql = textBox1.Text;
            inSql = inSql.ToUpper();

            selectStart = inSql.IndexOf("SELECT");
            fromStart = inSql.IndexOf("FROM");
            selectStart = selectStart + 6;
            firstPart = inSql.Substring(selectStart, (fromStart - selectStart));
            lastPart = inSql.Substring(fromStart, inSql.Length - fromStart);

            fields = firstPart.Split(',');
            firstPart = "";
            for (i = 0; i <= fields.Length - 1; i++)
            {
                if (i > 0)
                {
                    firstPart = firstPart + ", " + fields[i].ToString() + " AS COLUMN" + (i + 1);
                    firstPart.Trim();

                    MyText = (TextObject) objRpt.ReportDefinition.ReportObjects[i+1];
                    MyText.Text = fields[i].ToString();
                }
                else
                {
                    firstPart = firstPart + fields[i].ToString() + " AS COLUMN" + (i + 1);
                    firstPart.Trim();

                    MyText = (TextObject)objRpt.ReportDefinition.ReportObjects[i+1];
                    MyText.Text = fields[i].ToString();
                }
            }
            sql = "SELECT " + firstPart + " " + lastPart;
            return sql;
        }

Và viết code cho sư kiện của button :

private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cnn ;
            string connectionString = null;
            string sql = null;
            connectionString = "data source=SERVER NAME;initial catalog=crystaldb;user id=USER NAME;password=PASSWORD;";
            cnn = new SqlConnection(connectionString);
            cnn.Open();
            sql = procesSQL();
            SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
            DataSet1 ds = new DataSet1();
            dscmd.Fill(ds, "Product");
            objRpt.SetDataSource(ds.Tables[1]);
            crystalReportViewer1.ReportSource = objRpt;
            crystalReportViewer1.Refresh();
        }

À các bạn nhớ using mấy cái namespace cần thiết nhá
Còn 1 điều nữa là mọi vi dụ đều dùng database ở bài đâu,khi chạy ví dụ thì các bạn nhớ chỉnh lại chuỗi kết nối cho phù hợp
 

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Xây dựng Multiple Field Search nhờ thuộc tính Views Exposed Filters

Xây dựng Multiple Field Search nhờ thuộc tính Views Exposed Filters

Views offers the ability to expose filters to the end user so they may filter and sort through a views listing to find what they want in a large list of content.

Facebook

Mỗi ngày Facebook xử lí hơn 500 TB dữ liệu

Jay Parikh, Phó Chủ tịch phụ trách cơ sở hạ tầng kĩ thuật của Facebook đã thống kê một danh sách cho thấy số dữ liệu khổng lồ mà bộ phận này này phải xử lí mỗi ngày.

Integrated Device Technology, Inc. - IDT.com

Thiết kế Drupal site: www.idt.com

IDT required a solution that would prove to be fast, have an extremely easy-to-use interface, scale to thousands of products and offer the flexibility to grow and change quickly to keep pace with the company's markets

Công ty diệt chuột T&C

 

Diet con trung