본문 바로가기
IT 개발일지

C# datagridview 컬럼 셋팅 2가지

by (주)삼우기공 2020. 4. 13.
반응형

데이터 테이블을 사용하지 않고 컬럼 생성 코드

        public void colset(DataGridView datagrid ) 
        { 
            DataGridViewTextBoxColumn titleColumn = 
                new DataGridViewTextBoxColumn(); 
            titleColumn.HeaderText = "품명"; 
            titleColumn.Name = "품명"; 
            titleColumn.Width = 120; 
            titleColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None; 
            titleColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; 


            DataGridViewTextBoxColumn subTitleColumn = 
                new DataGridViewTextBoxColumn(); 
            subTitleColumn.HeaderText = "규격"; 
            subTitleColumn.Name = "규격"; 
            subTitleColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; 


            DataGridViewTextBoxColumn summaryColumn = 
                new DataGridViewTextBoxColumn(); 
            summaryColumn.HeaderText = "단위"; 
            summaryColumn.Name = "단위"; 
            summaryColumn.Width = 60; 
            summaryColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None; 
            summaryColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; 
            summaryColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; 

            DataGridViewTextBoxColumn contentColumn = 
                new DataGridViewTextBoxColumn(); 
            contentColumn.HeaderText = "수량"; 
            contentColumn.Name = "수량"; 
            contentColumn.Width = 60; 
            contentColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None; 
            contentColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; 
            contentColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; 

            DataGridViewTextBoxColumn amountColumn = 
                new DataGridViewTextBoxColumn(); 
            amountColumn.HeaderText = "매출업체"; 
            amountColumn.Name = "매출업체"; 
            amountColumn.Width = 100; 
            amountColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None; 
            amountColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; 
            amountColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; 

            DataGridViewTextBoxColumn descColumn = 
                new DataGridViewTextBoxColumn(); 
            descColumn.HeaderText = "비고"; 
            descColumn.Name = "비고"; 
            descColumn.Width = 120; 
            descColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None; 
            descColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; 
            descColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; 

            datagrid.Columns.AddRange(new DataGridViewTextBoxColumn[] { 
            titleColumn, subTitleColumn, 
            summaryColumn, contentColumn, amountColumn, descColumn }); 

            datagrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; 
        }

 

실제실행 화면

 

반응형

 

DB SELECT -  데이터 테이블을 사용하여 데이터 호출해서 컬럼 조정

               //datagrid.ReadOnly = true;
                // row 전체 선택
                //datagrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                // column 오토사이즈
                datagrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                datagrid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; // 발주번호
                datagrid.Columns[0].Width = 100;
                datagrid.Columns[0].Visible = false;
                datagrid.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; // 항번
                datagrid.Columns[1].Width = 60;
                datagrid.Columns[1].Visible = false;
                datagrid.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; // 품명
                datagrid.Columns[2].Width = 120;
                datagrid.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; // 수량
                datagrid.Columns[4].Width = 40;
                datagrid.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; // 단위
                datagrid.Columns[5].Width = 40;
                datagrid.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; // 비고
                datagrid.Columns[6].Width = 80;
                datagrid.Columns[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; // 매출업체
                datagrid.Columns[7].Width = 80;

                // 내용 가운데 정렬
                datagrid.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                datagrid.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                datagrid.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                datagrid.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                datagrid.Columns[6].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                datagrid.Columns[7].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

                // 헤더 가운데 정렬
                datagrid.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                datagrid.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                datagrid.Columns[2].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                datagrid.Columns[3].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                datagrid.Columns[4].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                datagrid.Columns[5].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                datagrid.Columns[6].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                datagrid.Columns[7].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

                //datagrid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                // 홀수열 색상변경
                //datagrid.AlternatingRowsDefaultCellStyle.BackColor = Color.Yellow;

 

실제실행 화면

 

반응형

댓글