ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Example of adding, deleting, and modifying new rows in .NET DataGridView
    IT Story/C# & WPF 2022. 6. 19. 21:21
    반응형
    public void subAddRow(DataGridView grdDataGridView, string[] sArrRowData)
            {
                BindingSource dBS = null;
                DataTable dDT = null;
    
                try
                {
                    //DataGridView의 컬럼개수와 새로 입력하려고 하는 컬럼데이터 개수가 맞지 않으면 빠져나간다.
                    if (grdDataGridView.ColumnCount != sArrRowData.Length) return;
    
                    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;
                    }
    
                    dDT.NewRow();
                    dDT.Rows.Add(sArrRowData);
                }
                catch
                {
                    throw new Exception();
                }
            }
    
            
            public void subDeleteRow(DataGridView grdDataGridView, int nRowIndex)
            {
                try
                {
                    this.pgrdDataGridView = grdDataGridView;
                    this.pgrdDataGridView.Rows.RemoveAt(nRowIndex);
                }
                catch
                {
                    throw new Exception();
                }
            }
    
            
            public void subUpdateRow(DataGridView grdDataGridView, int nRowIndex, string[] sArrRowData)
            {
                BindingSource dBS = null;
                DataTable dDT = null;
                try
                {
                    //DataGridView의 컬럼개수와 새로 입력하려고 하는 컬럼데이터 개수가 맞지 않으면 빠져나간다.
                    if (grdDataGridView.ColumnCount != sArrRowData.Length) return;
            
                    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;
                    }
    
                    //열너비를 수동조정해 GridView의 너비에 맞춘다.
                    for (int nLoop = 0; nLoop < sArrRowData.Length; nLoop++)
                    {
                        dDT.Rows[nRowIndex][nLoop] = sArrRowData[nLoop];
                    }
                }
                catch
                {
                    throw new Exception();
                }
            }
    반응형

    댓글

Designed by Tistory.