ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Example of changing the CellStyle of Row in .NET DataGridView
    IT Story/C# & WPF 2022. 6. 19. 21:23
    반응형
    public void subResizeColumnWidth(DataGridView grdDataGridView, params int[] nArrColumnWidth)
            {
                try
                {
                    //DataGridView의 컬럼개수와 설정하려고 하는 ColumnWidth 개수가 맞지 않으면 빠져나간다.
                    if (grdDataGridView.ColumnCount != nArrColumnWidth.Length)
                    {
                        return;
                    }
    
                    this.pgrdDataGridView = grdDataGridView;
    
                    // 열너비를 수동조정해 GridView의 너비에 맞춘다.
                    for (int nLoop = 0; nLoop < nArrColumnWidth.Length; nLoop++)
                    {
                        this.pgrdDataGridView.Columns[nLoop].Width = Convert.ToInt32(nArrColumnWidth[nLoop]);
                    }
                }
                catch
                {
                    throw new Exception();
                }
            }
    
    
            
            public void subChangeRowStyle(DataGridView grdDataGridView, int nStartRowIndex, int nEndRowIndex, Color cBackColor)
            {
                try
                {
                    Color dcForeColor = grdDataGridView.ForeColor;
                    FontStyle dfsFontStyle = grdDataGridView.Font.Style;
    
                    //아래 Overloading되어 있는 함수를 호출한다.
                    subChangeRowStyle(grdDataGridView, nStartRowIndex, nEndRowIndex, cBackColor, dcForeColor, dfsFontStyle);
                }
                catch
                {
                    throw new Exception();
                }
            }
    
            
            public void subChangeRowStyle(DataGridView grdDataGridView, int nStartRowIndex, int nEndRowIndex, Color cBackColor, Color cForeColor)
            {
                try
                {
                    FontStyle dfsFontStyle = grdDataGridView.Font.Style;
    
                    //아래 Overloading되어 있는 함수를 호출한다.
                    subChangeRowStyle(grdDataGridView, nStartRowIndex, nEndRowIndex, cBackColor, cForeColor, dfsFontStyle);
                }
                catch
                {
                    throw new Exception();
                }
            }
    
            
            public void subChangeRowStyle(DataGridView grdDataGridView, int nStartRowIndex, int nEndRowIndex, Color cBackColor, Color cForeColor, FontStyle fsFontStyle)
            {
                try
                {
                    this.pgrdDataGridView = grdDataGridView;
    
                    DataGridViewCellStyle dCellStype = new DataGridViewCellStyle();     //적용할 CellStyle을 생성한다.
                    dCellStype.BackColor = cBackColor;
                    dCellStype.ForeColor = cForeColor;
                    dCellStype.Font = new Font(this.pgrdDataGridView.Font, fsFontStyle);
    
                    //StartRowIndex, EndRowIndex 모두 -1이면 전체 행을 변경한다.
                    if (nStartRowIndex == -1 && nEndRowIndex == -1)
                    {
                        nStartRowIndex = 0;
                        nEndRowIndex = this.pgrdDataGridView.RowCount - 1;
                    }
    
                    for (int nLoop = nStartRowIndex; nLoop <= nEndRowIndex; nLoop++)
                    {
                        if (nLoop >= this.pgrdDataGridView.RowCount - 1)     //만약 RowIndex범위를 벗어나면 루프를 빠져나간다.
                        {
                            break;
                        }
    
                        this.pgrdDataGridView.Rows[nLoop].DefaultCellStyle = dCellStype;     //Row에 CellStype 적용
                    }
                }
                catch
                {
                    throw new Exception();
                }
            }
    반응형

    댓글

Designed by Tistory.