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

 
Một số điểm cơ bản về cơ chế tấn công SQL Injection và DDoS

Một số điểm cơ bản về cơ chế tấn công SQL Injection và DDoS

Trong phần lớn người sử dụng chúng ta, chắc hẳn nhiều người đã nghe nói tới khái niệm tấn công, chiếm quyền điều khiển website bằng phương pháp SQL Injection ...

Best Calorie Counter Application For Android Phones

Best Calorie Counter Application For Android Phones

Many more people all over the world are becoming cautious about the food they eat. If you are one of those who are very keen on their diet or weight then this is the app for you.

Adding a WYSIWYG Editor to Your Drupal Site

Thêm WYSIWYG Editor trong Drupal Site của bạn

WYSIWYG stands for "What You See Is What You Get" ... if you select some text and click the "bold" button, that text will be bold. If you select some text and click the "italic" button, that text will be in italics. There's no messing around with HTML. That will be a relief to some of you and more particularly ... your clients and site visitors.

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

 

Diet con trung