ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Example of entering, deleting, and modifying new columns in .NET DataGridView
    IT Story/C# & WPF 2022. 6. 19. 21:22
    반응형
    public void subAddColumn(DataGridView grdDataGridView, string sColumnName)
            {
                try
                {
                    int nColumnIndex = grdDataGridView.Columns.Count;        //컬럼을 Grid의 제일 뒤(오른쪽)에 추가한다.
                    string sColumnHeaderText = "ColumnHeader" + nColumnIndex.ToString();  //ColumnName
                    Color dcColumnColor = grdDataGridView.DefaultCellStyle.BackColor;
                    
                    //아래 Overloading되어 있는 함수를 호출한다.
                    subAddColumn(grdDataGridView, sColumnName, nColumnIndex, sColumnHeaderText, dcColumnColor, CellType.TextBox);
                }
                catch
                {
                    throw new Exception();
                }
            }
    
            
            public void subAddColumn(DataGridView grdDataGridView, string sColumnName, int nColumnIndex)
            {
                try
                {
                    string sColumnHeaderText = "ColumnHeader" + grdDataGridView.Columns.Count.ToString();  //ColumnName
                    Color dcColumnColor = grdDataGridView.DefaultCellStyle.BackColor;
    
                    //아래 Overloading되어 있는 함수를 호출한다.
                    subAddColumn(grdDataGridView, sColumnName, nColumnIndex, sColumnHeaderText, dcColumnColor, CellType.TextBox);
                }
                catch
                {
                    throw new Exception();
                }
            }
    
            
            public void subAddColumn(DataGridView grdDataGridView, string sColumnName, int nColumnIndex, string sColumnHeaderText)
            {
                try
                {
                    Color dcColumnColor = grdDataGridView.DefaultCellStyle.BackColor;
    
                    //아래 Overloading되어 있는 함수를 호출한다.
                    subAddColumn(grdDataGridView, sColumnName, nColumnIndex, sColumnHeaderText, dcColumnColor, CellType.TextBox);
                }
                catch
                {
                    throw new Exception();
                }
            }
    
            
            public void subAddColumn(DataGridView grdDataGridView, string sColumnName, int nColumnIndex, string sColumnHeaderText, Color cColumnColor, CellType ctCellType)
            {
                BindingSource dBS = null;
                DataTable dDT = null;
                DataGridViewColumn dDC = null;
                DataGridViewCell cell = null;
    
                try
                {
                    this.pgrdDataGridView = grdDataGridView;
    
    
                    if (this.pgrdDataGridView.DataSource.GetType().Name == "DataTable")
                    {
                        dDT = (DataTable)this.pgrdDataGridView.DataSource;
                    }
                    else if (this.pgrdDataGridView.DataSource.GetType().Name == "BindingSource")
                    {
                        //DataGridView에 바인딩된 객체가 BindingSource이면 BindingSource로 형변환 후 DataTable을 가져온다.
                        dBS = (BindingSource)this.pgrdDataGridView.DataSource;
                        dDT = (DataTable)dBS.DataSource;
                    }
    
                    switch (ctCellType)
                    {
                        case CellType.TextBox:
                            dDC = new DataGridViewTextBoxColumn();
                            cell = new DataGridViewTextBoxCell();
                            break;
    
                        case CellType.CheckBox:
                            dDC = new DataGridViewCheckBoxColumn();
                            cell = new DataGridViewCheckBoxCell();
                            break;
    
                        case CellType.Image:
                            dDC = new DataGridViewImageColumn();
                            cell = new DataGridViewImageCell();
                            break;
    
                        case CellType.Button:
                            dDC = new DataGridViewButtonColumn();
                            cell = new DataGridViewButtonCell();
                            break;
    
                        case CellType.ComboBox:
                            dDC = new DataGridViewComboBoxColumn();
                            cell = new DataGridViewComboBoxCell();
                            break;
    
                        case CellType.Link:
                            dDC = new DataGridViewLinkColumn();
                            cell = new DataGridViewLinkCell();
                            break;
    
                        default:
                            break;
                    }
                    
                    dDC.Name = sColumnName;                           //ColumnName
                    dDC.HeaderText = sColumnHeaderText;               //Column HeaderText
    
                    cell.Style.BackColor = cColumnColor;
                    dDC.CellTemplate = cell;
    
                    this.pgrdDataGridView.Columns.Insert(nColumnIndex, dDC);
                }
                catch(Exception)
                {
                    throw new Exception();
                }
            }
    반응형

    댓글

Designed by Tistory.