วันพุธที่ 9 ตุลาคม พ.ศ. 2556

Method Display Data In DataGridView (C#)

เป็นการแสดงข้อมูลน ตาราง DataGridView แบบที่ 1 For Loop แสดง

แบบที่ 1. เป็นการแสดง ออก DataGridView ทีละ บรรทัด DataRow 

โดยประกอบด้วย
   1.Method สำหรับ แสดงข้อมูลออกตาราง DataGridView โดยมี ตรวจสอบว่า มีข้อมูลในอีกตารางหรือไม่ ถ้าไม่มี ให้ทำการ แสดงข้อมูลออกตารางเป็นสีแดง ถ้ามี ให้แสดง เป็นสีดำ

        public void showTable(String Dept_no)
        {
            String isSQL = "Select t.Topic_code,t.Topic_name,d.Dept_name,t.pln_from,t.pln_to"
                            + " from ITR_Topic_db t left join ITR_Dept_db d "
                            + " on t.Dept = d.Dept_no where Dept = '" + Dept_no + "' and T.act <> 'D'  and ( "
                            + "(Select COUNT(*) from ITR_Details d"
                            + " where d.Topic_code = t.Topic_code and t.act <> 'D' and d.act <> 'D' and Result = '-') > 0 "
                            + " or (Select Count(*) from ITR_Details d"
                            + " where d.Topic_code = t.Topic_code and d.act <> 'D' and d.Result  in('-','1','2','3','4','5','6','7')) = 0 )"
                            + " Order by t.Topic_code";

          ConnectDatabase ConDb = new ConnectDatabase();
          DataSet DataTable1 = ConDb.Query_All(isSQL, "ITR_Topic_db");

            int i = 0;
            DataGridView.Rows.Clear();
            foreach (DataRow theRow in DataTable1.Tables["ITR_Topic_db"].Rows)
            {
                this.DataGridView.Rows.Add();
                if (chekResult(theRow[0].ToString()).Equals("0"))
                {
                    this.DataGridView.Rows[i].Cells[0].Style.ForeColor = Color.Red;
                    this.DataGridView.Rows[i].Cells[0].Style.SelectionForeColor = Color.Red;
                }
                else
                {
                    this.DataGridView.Rows[i].Cells[0].Style.ForeColor = Color.Black;
                    this.DataGridView.Rows[i].Cells[0].Style.SelectionForeColor = Color.White;
                }
                this.DataGridView.Rows[i].Cells[0].Value = theRow[0].ToString();
                this.DataGridView.Rows[i].Cells[1].Value = theRow[1].ToString();
                this.DataGridView.Rows[i].Cells[2].Value = theRow[2].ToString();
                this.DataGridView.Rows[i].Cells[3].Value = theRow[3].ToString();      
                this.DataGridView.Rows[i].Cells[4].Value = theRow[4].ToString();
                i++;
            }
        }
__________________________________________________________________________
                    1.2. Method สำหรับตัวตรวจสอบ ว่ามีข้อมูลในอีก ตาราง หรือไม่
                                         ConnectDatabase ConDb = new ConnectDatabase();
                                         private String chekResult(String topic_code)
                                                {
                                                            return ConDb.Query_one("Select count(*) from ITR_details where Act <> 'D' and                                                             Topic_code = '" + topic_code + "'", "ITR_details").ToString();
                                                  }                    

___________________________________________________________________

2. แบบที่ 2 ใช้ Method  DataSource ของ DatagridView ในการแสดง 

                 ConnectDatabase ConDb = new ConnectDatabase();
                       public void showTable(String isSQL, String DB)
                             {
                                 DataSet DataTable1 = ConDb .Query_All(isSQL, DB);

                                 DataGridView.DataSource = null;

                                 this.table_listemp.DataSource = DataTable1.Tables[0]; ;

                                 DataGridView.Columns[0].Width = 30;
                                 DataGridView.Columns[1].Width = 100;
                                 DataGridView.Columns[2].Width = 100;
                                 DataGridView.Columns[3].Width = 300;
                                 DataGridView.Columns[0].HeaderText = "Check";
                                 DataGridView.Columns[1].HeaderText = "ชื่อ";
                                 DataGridView.Columns[2].HeaderText = "นามสกุล";
                                 DataGridView.Columns[3].HeaderText = "Grade Name";

                    }

ไม่มีความคิดเห็น:

แสดงความคิดเห็น