Home > .Net Framework > Kelas untuk memanipulasi DataGridView di .Net Framework

Kelas untuk memanipulasi DataGridView di .Net Framework


Di .Net Framework disediakan sebuah komponen untuk menampilkan data dalam bentuk tabel, namun untuk menggunakannya tidak semudah Component One Flex Grid, oleh karena itu berikut ini disajikan sebuah kelas untuk memanipulasi DataGridView

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
namespace NetFrameWork.Function
{
    public class GridFunction : MyFunction
    {
        private short c;
        private int colCount,number;
        private short pj;
        private DataGridView dg;
        public GridFunction(DataGridView dg)
        {
            this.dg = dg;
        }
        public void setDataGrid(DataTable dt)
        {
            setDataGrid(-1, dt);
        }
        public void addRow()
        {
            dg.Rows.Add();
        }
        public void clear()
        {
            dg.Rows.Clear();
        }
        public void setDataGrid(short beginCol, DataTable dt)
        {
            dg.DataSource = dt;
            c = 0;
            foreach (DataGridViewColumn dgvc in dg.Columns)
            {
                if (c != beginCol && c < dg.Columns.Count)
                {                    
                    dgvc.HeaderText = UpperFirstCharOnWord(dg.Columns[c].HeaderText);
                    //Application.DoEvents();
                }
                c++;
            }
            dg.MultiSelect = false;
            dg.AllowUserToAddRows = false;
            dg.AllowUserToDeleteRows = false;
            dg.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dg.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dg.EditMode = DataGridViewEditMode.EditProgrammatically;
            
            
        }

        public void addCheckBoxColumn(string header,bool readOnly)
        {
            if (!dg.Columns.Contains(header.Trim()))
            {
                DataGridViewCheckBoxColumn checkBox = new DataGridViewCheckBoxColumn();
                checkBox.HeaderText = header.Trim();
                checkBox.Name = header.Trim();
                checkBox.ReadOnly = readOnly;
                    
                dg.Columns.Add(checkBox);


            }

            
            //DataGridViewColumn column = dg.Columns[header.Trim()];
            //column.ValueType = typeof(bool);

           
        }

        public void setDataGridManual(short beginCol, DataTable dt)
        {
            dg.Rows.Clear();            
            int i = 0;
         
            foreach (DataGridViewColumn dgvc in dg.Columns)
            {
                if (c != beginCol && c<dg.Columns.Count )
                {
                    dgvc.HeaderText = UpperFirstCharOnWord(dg.Columns[c].HeaderText);
                    Application.DoEvents();
                }
                c++;
            }
            //dg.MultiSelect = false;
            //dg.AllowUserToAddRows = false;
            //dg.AllowUserToDeleteRows = false;
            dg.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dg.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            //dg.EditMode = DataGridViewEditMode.EditProgrammatically;
            colCount = dg.ColumnCount;
            if (dt.Rows.Count == 0)
                return;
            foreach (DataRow dr in dt.Rows)
            {
                object[] obj = new object[colCount];
                for (i = (beginCol); i <= colCount; i++)
                {
                    int j = i - 1;
                    if (j < dt.Columns.Count)
                    {
                        obj[j] = dr[j].ToString().Trim();
                    }
                    
                }
                dg.Rows.Add(obj);
                //dg.Rows[dg.Rows.Count - 1].ReadOnly = true;    
            }
            
        }
        /// <summary>
        /// Lakukan pengesetan autonumber pada DataGridView
        /// </summary>
        public void AutoNumber()
        {
            colCount = dg.Rows.Count;
            int i;
            for (i = 0; i < colCount; i++)
            {
                contentModify(0,i, (i+1).ToString());
            }
        }

        
        /// <summary>
        /// Atur lebar kolom pada DataGridView
        /// </summary>
        /// <param name="colWidth">lebar kolom, contoh 30,30,30</param>
        public void setColWidth(params int[] colWidth)
        {
            dg.AllowUserToResizeColumns = true;
            c = 0;
            foreach (DataGridViewColumn dgvc in dg.Columns)
            {
                if (c < colWidth.Length)
                {
                    dgvc.Width = colWidth[c];
                    c++;
                }
                else
                    break;
            }            
            dg.AllowUserToResizeColumns = false;
        }

        public void setColWidth(string[] header, params int[] colWidth)
        {
            dg.AllowUserToResizeColumns = true;
            for (int i = 0; i < header.Length; i++)
            {
                dg.Columns[header[i]].Width = colWidth[i];
            }
            dg.AllowUserToResizeColumns = false;
        }
        /// <summary>
        /// Sembunyikan kolom
        /// </summary>
        /// <param name="hideCol">kolom yang akan disembunyikan, contoh 
        /// 30,30,30</param>
        public void hideCol(params int[] hideCol)
        {

            pj = short.Parse(hideCol.Length.ToString());
            for (c = 0; c < pj; c++)
            {
                dg.Columns[hideCol[c]].Visible = false;
            }
        }

        public void hideCol(params string[] hideCol)
        {
            pj = short.Parse(hideCol.Length.ToString());
            for (c = 0; c < pj; c++)
            {
                dg.Columns[hideCol[c]].Visible = false;
            }
        }
        public void headerModify(string headerValue,byte col)
        {
            if (dg.ColumnCount == 0)
            {
                return;
            }
            dg.Columns[col].HeaderText = headerValue.Trim();            
        }
        public void headerModify(int[] col, params string[] headerValue)
        {
            pj = (short)col.Length;
            for (c = 0; c < pj; c++)
            {
                dg.Columns[col[c]].HeaderText = headerValue[c];
            }            
        }
        public void textAlligment(DataGridViewContentAlignment pos,byte col)
        {
            DataGridViewCellStyle dcs = new DataGridViewCellStyle();
            dcs.Alignment = pos;
            dg.Columns[col].DefaultCellStyle = dcs;            
        }
        public void contentModify(int col,int row, string data)
        {
            dg[col, row].Value = data;
        }

    

        public void contentModify(int col, string oldData,string newData)
        {
            foreach (DataGridViewRow dcr in dg.Rows)
            {
                if (dg[col, dcr.Index].Value.ToString().Trim() == oldData.Trim())
                {                    
                    dg[col, dcr.Index].Value = newData;
                }
            }
        }
        public void gridToDetail(params Control[] ctrl)
        {            
            colCount = ctrl.Length;            
            for (c = 0; c < colCount; c++)
            {
                if (ctrl[c] != null)
                {
                    if (dg.SelectedCells[c].Value.ToString().Trim().Length > 0)
                    {
                        object data = dg.SelectedCells[c].Value;
                        switch (ctrl[c].GetType().Name)
                        {
                            case "TextBox" :
                                tempTxt = (TextBox)ctrl[c];
                                tempTxt.Text = data.ToString().Trim();
                                break;
                            case "ComboBox":
                                tempCb = (ComboBox)ctrl[c];
                                tempCb.Text = data.ToString().Trim(); 
                                break;
                            case "DateTimePicker":
                                tempDt = (DateTimePicker)ctrl[c];
                                tempDt.Value = (DateTime)dg.SelectedCells[c].Value;                                
                                break;
                            case "Label":
                                tempLbl = (Label)ctrl[c];
                                tempLbl.Text = data.ToString().Trim();
                                break;
                            //case "OptionBox":
                            //    tempOption = (r47m87.WinForm.OptionBox)ctrl[c];
                            //    tempOption.kananTerpilih = !textToBoolean("YA", data.ToString().Trim());
                            //    break;
                        }
                    }
                }
            }
        }
    }

}

Categories: .Net Framework Tags: , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: