본문 바로가기

Computerscience/C#

DatagrideView에 쿼리 반환값 넣기[기초]



/* 이전 생략 */


 DataTable oDataTable = new DataTable();

            DataColumn col1 = new DataColumn();
            col1.ColumnName = "예약번호";
            col1.DataType = System.Type.GetType("System.String");

            DataColumn col2 = new DataColumn();
            col2.ColumnName = "출항일자";
            col2.DataType = System.Type.GetType("System.String");

            DataColumn col3 = new DataColumn();
            col3.ColumnName = "출항시간";
            col3.DataType = System.Type.GetType("System.String");

            DataColumn col4 = new DataColumn();
            col4.ColumnName = "선박명";
            col4.DataType = System.Type.GetType("System.String");

            DataColumn col5 = new DataColumn();
            col5.ColumnName = "객실등급";
            col5.DataType = System.Type.GetType("System.String");
            oDataTable.Columns.Add(col1);
            oDataTable.Columns.Add(col2);
            oDataTable.Columns.Add(col3);
            oDataTable.Columns.Add(col4);
            oDataTable.Columns.Add(col5);

            DataRow nRow;

            String str = "";

            if (bcheckIsConnection)
            {
                SqlCommand oCmm_getlist = new SqlCommand();
                oCmm_getlist.CommandType = CommandType.Text;
                oCmm_getlist.Connection = oCnn;
                oCmm_getlist.CommandText = "select b.booking_no, a.departure_expect_dt, a.departure_expect_tm, b.w_vessel_cd, b.w_class_cd from tc_voyage a, " +
                "tp_sales b  where a.vessel_cd = b.w_vessel_cd and a.voyage_no = b.w_voyage_no and b.passport_no = '" + txtpassport.Text + "' and b.booking_password" +
                "= '" + txtbookingpw.Text + "' and b.kiosk_yn = 'Y'";

                SqlDataAdapter oAdapter = new SqlDataAdapter();
                DataSet oDataSet = new DataSet();
                oAdapter.SelectCommand = oCmm_getlist;
                oAdapter.Fill(oDataSet);

                try
                {
                    for (int i = 0; i < oDataSet.Tables[0].Rows.Count; i++)
                    {
                        nRow = oDataTable.NewRow();
                        for (int j = 0; j < 5; j++)
                        {
                            str = oDataSet.Tables[0].Rows[i][j].ToString();
                            switch (j)
                            {
                                case 0:
                                    nRow["예약번호"] = str;
                                    break;
                                case 1:
                                    nRow["출항일자"] = str;
                                    break;
                                case 2:
                                    nRow["출항시간"] = str;
                                    break;
                                case 3:
                                    nRow["선박명"] = str;
                                    break;
                                case 4:
                                    nRow["객실등급"] = str;
                                    break;
                                default:
                                    break;
                            }
                        }
                        oDataTable.Rows.Add(nRow);
                    }
                    dataGridView1.DataSource = oDataTable;

                   /* 이후 생략 */



아주 기초적인 방법.. 데이터 테이블을 생성 -> 테이블 컬럼 생성 -> 로우 생성 -> 로우의 컬럼에
값을 넣고 데이터 테이블 객체에 로우를 Add한후 데이터 그리드 뷰의 데이터 소스에 테이블 객체를
넣어주면 된다.. 음.. 일련의 과정들이 너무 노가다다.. 다음엔 SQLHelper 클래스를 이용해서
그리드 뷰에 표시하는 방법을..^^

'Computerscience > C#' 카테고리의 다른 글

c# 훅크 사용법[스크랩]  (0) 2009.08.13
DB 연동하기  (0) 2009.02.13
메세지박스 활용법  (0) 2008.09.04
C# 스토워드 프로시져로 쿼리 날리기.  (0) 2008.09.03
C#과 C++ 문법적으로 다른점  (0) 2007.11.23