Tuesday, September 14, 2010

How to Paint a Row in DataGridView in C# Window Application, How To Insert Disable Button Column DataGridView in C# Window Application, How to Merge Rows in DatagridView DataGridView in C# Window Application

//Create a Window Application in C# and add a user control on that form name "ucDemoGrid" and write the code given below for each function

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataGridViewApplication
{
    public partial class ucDemoGrid : UserControl
    {
        // Declare all the variables for the grid control
        #region Variables
        Random rnd = new Random();
        public DataTable dt = new DataTable();
        double empCodes = 0.0;
        double UsedMargin = 0.0;
        double Total = 00.0;
        public int FixedRow = 0;
        public int MergeRow = 0;
        public int DepositRow = 0;
        private int colIndex = 0;
        private int firstColmIndex = 0;
        private int lastColmIndex = 0;
        private int mergeCount = 0;
        private int lastrowindex = 0;
        private int mergeWidth = 0;
        private string MergeText = string.Empty;
        private ContextMenuStrip gridContextmenu = new ContextMenuStrip();
        List<int> DepRow = new List<int>();
        DataGridViewRow dr;
        DataGridViewCell dc;
        #endregion

        #region Property
        //Assing the name to the gridcontrol : _DemoGrid
        private DataGridView _DemoGrid;
        public DataGridView DemoGrid
        {
            get { return _DemoGrid; }
            set { _DemoGrid = value; }
        }
        #endregion

        #region Constructor
        public ucDemoGrid()
        {
            InitializeComponent();
            _DemoGrid = dgvSample; // pass the grid control "dgvSample" refrerring to _DemoGrid.
        }
        #endregion


        //Make the Custom Properties of the grid Control so that we can change the color of grid control from outside options
        #region GridColorSettingProperties

        public Color setColor
        {
            get { return _DemoGrid.BackgroundColor; }
            set { _DemoGrid.BackgroundColor = value; }
        }
        public Color setDefaultCellStyleBackColor
        {
            get { return _DemoGrid.DefaultCellStyle.BackColor; }
            set { _DemoGrid.DefaultCellStyle.BackColor = value; }
        }
        public Color setDefaultCellStyleForeColor
        {
            get { return _DemoGrid.DefaultCellStyle.ForeColor; }
            set { _DemoGrid.DefaultCellStyle.ForeColor = value; }
        }

        public Font setDefaultFontMarketWatch
        {
            get { return _DemoGrid.DefaultCellStyle.Font; }
            set { _DemoGrid.DefaultCellStyle.Font = value; }
        }
        public Font setRowsDefaultCellStyleFontMarketWatch
        {
            get { return _DemoGrid.RowsDefaultCellStyle.Font; }
            set { _DemoGrid.RowsDefaultCellStyle.Font = value; }
        }
        public Font setAlternateFontMarketWatch
        {
            get { return _DemoGrid.AlternatingRowsDefaultCellStyle.Font; }
            set { _DemoGrid.AlternatingRowsDefaultCellStyle.Font = value; }
        }
        public Font SetHeaderFont
        {
            get { return _DemoGrid.ColumnHeadersDefaultCellStyle.Font; }
            set { _DemoGrid.ColumnHeadersDefaultCellStyle.Font = value; }
        }


        public Color setAlternateRowBackColor
        {
            get { return _DemoGrid.AlternatingRowsDefaultCellStyle.BackColor; }
            set { _DemoGrid.AlternatingRowsDefaultCellStyle.BackColor = value; }
        }
        public Color setAlternateRowForeColor
        {
            get { return _DemoGrid.AlternatingRowsDefaultCellStyle.ForeColor; }
            set { _DemoGrid.AlternatingRowsDefaultCellStyle.ForeColor = value; }
        }
        public Color setSelectionBackColor
        {
            get { return _DemoGrid.DefaultCellStyle.SelectionBackColor; }
            set { _DemoGrid.DefaultCellStyle.SelectionBackColor = value; }
        }
        public Color setSelectionForeColor
        {
            get { return _DemoGrid.DefaultCellStyle.SelectionForeColor; }
            set { _DemoGrid.DefaultCellStyle.SelectionForeColor = value; }
        }
        public Color setGridLinesColor
        {
            get { return _DemoGrid.GridColor; }
            set { _DemoGrid.GridColor = value; }
        }
        public Color SetHeaderBackColor
        {
            get { return _DemoGrid.ColumnHeadersDefaultCellStyle.BackColor; }
            set { _DemoGrid.ColumnHeadersDefaultCellStyle.BackColor = value; }
        }
        public Color SetHeaderForeColor
        {
            get { return _DemoGrid.ColumnHeadersDefaultCellStyle.ForeColor; }
            set { _DemoGrid.ColumnHeadersDefaultCellStyle.ForeColor = value; }
        }


        #endregion

        //here are some formulas to calculate width and no of columns of the grid of specific rows or normal calculations
        #region Formulas & Function
        private int TotalWidthOfColumn(DataGridView dgv)
        {
            int RowCnt = dgv.ColumnCount;
            int iWidth = 0;
            for (int i = 0; i < RowCnt; i++)
            {
                iWidth += dgv.Columns[i].Width;
            }
            return iWidth;

        }
        private int TotalWidthOfColumn(DataGridView dgv, int[] Column2Count)
        {
            int RowCnt = Column2Count.Length;
            int iWidth = 0;
            for (int i = 0; i < RowCnt; i++)
            {
                iWidth += dgv.Columns[Column2Count[i]].Width;
            }
            return iWidth;

        }
        private int TotalWidthOfColumn(DataGridView dgv, int Column2Count)
        {
            for (int i = firstColmIndex; i < lastColmIndex; i++)
            {
                mergeWidth += dgv.Columns[i].Width;
            }
            return mergeWidth;
        }

        private int SetxIndex4Total(string colmName)
        {
            colIndex = _DemoGrid.Columns[colmName].Index;
            return colIndex;
        }
        private int SetFirstIndex(string colmName)
        {
            firstColmIndex = _DemoGrid.Columns[colmName].Index;
            return firstColmIndex;
        }
        private int SetLastIndex(string colmName)
        {
            lastColmIndex = _DemoGrid.Columns[colmName].Index;
            return lastColmIndex;
        }
        private int col2Count(int firstIndex, int lastIndex)
        {
            mergeCount = lastColmIndex - firstColmIndex;
            return mergeCount;
        }
        #endregion

        //This paint method is used to display the static "Fixed Row" a gray color row
        private void dgvSample_Paint(object sender, PaintEventArgs e)
        {
            Rectangle rct = _DemoGrid.GetRowDisplayRectangle(FixedRow, false);
            if (rct.Left == 0 && rct.Right == 0 && rct.Height == 0 && rct.Width == 0)
            {
                //display nothing
            }
            else
            {

                SizeF s = e.Graphics.MeasureString("RO", _DemoGrid.Font);
                rct.X = _DemoGrid.GetColumnDisplayRectangle(0, false).X;
                rct.Y = _DemoGrid.GetRowDisplayRectangle(FixedRow, false).Y;
                rct.Width = TotalWidthOfColumn(_DemoGrid);

                Rectangle rctTotal = _DemoGrid.GetRowDisplayRectangle(FixedRow, false);

                rctTotal.X = _DemoGrid.GetColumnDisplayRectangle(colIndex, false).X;
                float tops = rct.Top + ((rct.Height / 2) - (s.Height / 2));
                e.Graphics.FillRectangle(Brushes.White, rct);
                Font f = new Font("Verdana", 8, FontStyle.Bold);
                e.Graphics.FillRectangle(Brushes.Silver, rct);
                e.Graphics.DrawString("Total Emp Code : " + empCodes + "  Used Margin : " + UsedMargin, f, Brushes.Black, rct.X, rct.Y);
               //you can change the display string here....
               
                if (rctTotal.X == 0)
                {
                    //display nothing
                }
                else
                { //you can change the display string here....
                    e.Graphics.DrawString("Total  " + Total, f, Brushes.Black, rctTotal.X, rct.Y);
                    _DemoGrid.Rows[FixedRow].ReadOnly = true;

                }

            }
        }

        private void MergingRow()
        {
            //here we call the customized class object for the merging facility
            //here we can apply more than two (declared below) columns names for merging.
            //In the example we merge only the EmpName and Age Columns of the grid control

            DataGridViewAdvancedBorderStyle mystyle = new DataGridViewAdvancedBorderStyle();
            mystyle.All = DataGridViewAdvancedCellBorderStyle.None;
            DataGridViewAdvancedBorderStyle myplaceholder = new DataGridViewAdvancedBorderStyle();

            CustomDataGridViewCell cellName = new CustomDataGridViewCell();
            cellName.AdvancedBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.Single;
            _DemoGrid.Rows[lastrowindex].Cells["EmpName"] = cellName;

            CustomDataGridViewCell cellAge = new CustomDataGridViewCell();
            _DemoGrid.Rows[lastrowindex].Cells["Age"] = cellAge;
            cellAge.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Single;

        }

        public int InsertRow(int EmpCode, string EmpName, int Age, string Address, bool Check, string Save)
        {
            FixedRow++;
            lastrowindex = 0;
            _DemoGrid.Rows.Insert(lastrowindex, EmpCode, EmpName, Age, Address, false, Save);

            return lastrowindex;
        }

        private void btnGenerateMergedRow_Click(object sender, EventArgs e)
        {
            InsertRow(rnd.Next(1651, 5789), "Raj Pandey", rnd.Next(19, 28), "New Delhi", false, "Save");
            MergingRow();
        }

        private void btnLoadData_Click(object sender, EventArgs e)
        {
            InsertRow(rnd.Next(1651, 5789), "Ashish Pandey", rnd.Next(19, 28), "Lucknow", false, "Save");
        }

        private void dgvSample_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //on the header click show the customized context menu when the user right click on the header.
            if (e.Button == MouseButtons.Right)
            {
                dgvSample.ContextMenuStrip = contextMenuStripHeader;
            }
        }


        private void dgvSample_MouseUp(object sender, MouseEventArgs e)
        {
            //here we can check the Hit of the user on the grid control that where the user is currently right click
            //is it header or cell, Depend upon that the context menu will change

            if (e.Button == MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = _DemoGrid.HitTest(e.X, e.Y);

                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    this._DemoGrid.ContextMenuStrip = contextMenuStripCell;
                    contextMenuStripCell.Show(Control.MousePosition);
                    contextMenuStripCell.Closing += new ToolStripDropDownClosingEventHandler(contextMenuStripName_Closing);
                }
                if (hti.Type == DataGridViewHitTestType.ColumnHeader)
                {

                    dgvSample.ContextMenuStrip = contextMenuStripHeader;
                    contextMenuStripHeader.Show(Control.MousePosition);
                }
                else
                {
                    this.gridContextmenu.Hide();
                    this._DemoGrid.ContextMenuStrip = null;
                }
            }
        }
        //It is necessary method to close the open context menu
        void contextMenuStripName_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            _DemoGrid.ContextMenuStrip = null;
        }

        private void dgvSample_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            //It is used for every change in the datagridview
            if (_DemoGrid.IsCurrentCellDirty)
            {
                _DemoGrid.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void ucDemoGrid_Load(object sender, EventArgs e)
        {
            _DemoGrid.Rows.Add();

            #region ContextMenuCode
            // This code will generate the customized context menu which having facility of checkbox to select to Display the column header
            // or uncheck to hide the column header. But "EmpCode" column will not display in the context menu list
            foreach (DataGridViewColumn dgv in _DemoGrid.Columns)
            {
                if (dgv.Name != "EmpCode")
                {
                    ToolStripMenuItem ts = new ToolStripMenuItem(dgv.HeaderText, null, GeneralTSMI_Click);
                    ts.Checked = true;
                    contextMenuStripHeader.Items.Add(ts);
                }
                dgv.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            #endregion

            SetxIndex4Total("Address"); //You can set the position of Fixed Row Text "Total :" here by providing column name
            SetFirstIndex("EmpName");
            SetLastIndex("Age");
            col2Count(firstColmIndex, lastColmIndex);
            TotalWidthOfColumn(_DemoGrid, mergeCount);

            #region ContextMenuStrip
            ToolStripMenuItem tsGridLines;
            tsGridLines = new ToolStripMenuItem("Grid Lines");
            tsGridLines.Click += new EventHandler(GridLines);
            gridContextmenu.Items.Add(tsGridLines);
            #endregion
        }

        private void GeneralTSMI_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem t = (ToolStripMenuItem)sender;
            t.Checked = !t.Checked; ;
            _DemoGrid.Columns[t.Text].Visible = t.Checked;

        }

        private void GridLines(object sender, EventArgs e)
        {
            if (_DemoGrid.CellBorderStyle != DataGridViewCellBorderStyle.None)
            {
                _DemoGrid.CellBorderStyle = DataGridViewCellBorderStyle.None;
            }
            else
            {
                _DemoGrid.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            }
        }

        public void InsertDisableButtonRow(int EmpCode, string EmpName, int Age, string Address, bool Check, string Save)
        {
            lastrowindex = FixedRow + 1;
            _DemoGrid.Rows.Insert(lastrowindex, EmpCode, EmpName, Age, Address, false, Save);
        }
        private void btnDisableButtonRow_Click(object sender, EventArgs e)
        {
            InsertDisableButtonRow(rnd.Next(1651, 5789), "Ashish", rnd.Next(15,36), "Noida", false, "Save");

            DataGridViewCheckBoxCell chkcell = (DataGridViewCheckBoxCell)_DemoGrid.Rows[lastrowindex].Cells["Check"];
            DataGridViewDisableButtonCell buttonCellSave = new DataGridViewDisableButtonCell();
            _DemoGrid.Rows[lastrowindex].Cells["Save"] = buttonCellSave;

            chkcell.ReadOnly = true;
            buttonCellSave.Enabled = false;
        }

        private void btnGenerateEnableButtonRow_Click(object sender, EventArgs e)
        {
            btnLoadData_Click(sender, e);
        }
        private void horizontleLinesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._DemoGrid.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;// Right click on horizontle lines
        }
        private void verticalLinesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._DemoGrid.CellBorderStyle = DataGridViewCellBorderStyle.SingleVertical;// Right click on Vertical lines
        }
        private void fullGridToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._DemoGrid.CellBorderStyle = DataGridViewCellBorderStyle.Single;// Right click on Grid lines
        }
        private void hideGridLinesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._DemoGrid.CellBorderStyle = DataGridViewCellBorderStyle.None;// Right click on No grid lines
        }
       
        private void customizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmCustomize frm = new frmCustomize();
            frm.MyGrid = this;  //attach here the form object with the _Demo grid user control that passes all the refrences
            frm.ShowDialog();
        }

        public int InsertExecutedRow(int EmpCode, string EmpName, int Age, string Address, bool Check, string Save)
        {
            FixedRow++;

            int rowCountTotal = _DemoGrid.Rows.Count;
            int rowCountB4Fixed = rowCountTotal - FixedRow;

            lastrowindex = 0;
           _DemoGrid.Rows.Insert(lastrowindex, EmpCode, EmpName, Age, Address, Check, Save);
            return lastrowindex;
        }


        private void btnShiftRowAboveFixRow_Click(object sender, EventArgs e)
        {
            if (_DemoGrid.Rows.Count > FixedRow + 1)
            {
                dr = _DemoGrid.Rows[FixedRow + 1];
                dc = _DemoGrid.Rows[FixedRow + 1].Cells["EmpName"];
                _DemoGrid.Rows.Remove(dr);

                dc.Value = "Executed";
                _DemoGrid.Rows.Insert(FixedRow, dr);

                DataGridViewCheckBoxCell chkcell = (DataGridViewCheckBoxCell)_DemoGrid.Rows[FixedRow].Cells["Check"];
                DataGridViewDisableButtonCell buttonCellSave = new DataGridViewDisableButtonCell();
                _DemoGrid.Rows[FixedRow].Cells["Save"] = buttonCellSave;
              
                buttonCellSave.Enabled = true;
                chkcell.ReadOnly = false;

                FixedRow++;
            }
        }

        private void btnShiftRowbelowFixRow_Click(object sender, EventArgs e)
        {
            btnDisableButtonRow_Click(sender, e);
        }
    }
   
    #region Customize Classes for DatagridView
        public class CustomDataGridViewCell : DataGridViewTextBoxCell
        {
            private DataGridViewAdvancedBorderStyle _style;

            public CustomDataGridViewCell()
                : base()
            {
                _style = new DataGridViewAdvancedBorderStyle();
                _style.Bottom = DataGridViewAdvancedCellBorderStyle.Single;
                _style.Top = DataGridViewAdvancedCellBorderStyle.Single;
                _style.Left = DataGridViewAdvancedCellBorderStyle.None;
                _style.Right = DataGridViewAdvancedCellBorderStyle.None;


            }

            public DataGridViewAdvancedBorderStyle AdvancedBorderStyle
            {
                get { return _style; }
                set
                {
                    _style.Bottom = value.Bottom;
                    _style.Top = value.Top;
                    _style.Left = value.Left;
                    _style.Right = value.Right;
                }
            }

            protected override void PaintBorder(Graphics graphics, Rectangle clipBounds, Rectangle bounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle)
            {
                base.PaintBorder(graphics, clipBounds, bounds, cellStyle, _style);
            }

            protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
            {
                base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, _style, paintParts);
            }

        }
        public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn
        {
            public DataGridViewDisableButtonColumn()
            {
                this.CellTemplate = new DataGridViewDisableButtonCell();
            }
        }
        public class DataGridViewDisableButtonCell : DataGridViewButtonCell
        {
            private bool enabledValue;
            public bool Enabled
            {
                get
                {
                    return enabledValue;
                }
                set
                {
                    enabledValue = value;
                }
            }

            // Override the Clone method so that the Enabled property is copied.
            public override object Clone()
            {
                DataGridViewDisableButtonCell cell = (DataGridViewDisableButtonCell)base.Clone();
                cell.Enabled = this.Enabled;
                return cell;
            }

            // By default, enable the button cell.
            public DataGridViewDisableButtonCell()
            {
                this.enabledValue = false;
                this.Style.ForeColor = Color.Pink;
                this.Value = "Save";

            }

            protected override void Paint(Graphics graphics,
                Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
                DataGridViewElementStates elementState, object value,
                object formattedValue, string errorText,
                DataGridViewCellStyle cellStyle,
                DataGridViewAdvancedBorderStyle advancedBorderStyle,
                DataGridViewPaintParts paintParts)
            {
                // The button cell is disabled, so paint the border, 
                // background, and disabled button for the cell.
                if (!this.enabledValue)
                {
                    // Draw the cell background, if specified.-
                    if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
                    {
                        //SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);
                        SolidBrush cellBackground = new SolidBrush(Color.Blue);
                        graphics.FillRectangle(cellBackground, cellBounds);
                        cellBackground.Dispose();
                    }

                    // Draw the cell borders, if specified.
                    if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
                    {
                        PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
                    }

                    // Calculate the area in which to draw the button.
                    Rectangle buttonArea = cellBounds;
                    Rectangle buttonAdjustment = this.BorderWidths(advancedBorderStyle);
                    buttonArea.X += buttonAdjustment.X;
                    buttonArea.Y += buttonAdjustment.Y;
                    buttonArea.Height -= buttonAdjustment.Height;
                    buttonArea.Width -= buttonAdjustment.Width;

                    // Draw the disabled button.               
                    ButtonRenderer.DrawButton(graphics, buttonArea, System.Windows.Forms.VisualStyles.PushButtonState.Disabled);


                    // Draw the disabled button text.
                    if (this.FormattedValue is String)
                    {
                        TextRenderer.DrawText(graphics, (string)this.FormattedValue, this.DataGridView.Font, buttonArea, SystemColors.ActiveCaption);
                    }
                }
                else
                {
                    SolidBrush cellBackground = new SolidBrush(Color.DodgerBlue);
                    graphics.FillRectangle(cellBackground, cellBounds);
                    cellBackground.Dispose();

                    Rectangle buttonArea = cellBounds;
                    Rectangle buttonAdjustment = this.BorderWidths(advancedBorderStyle);
                    buttonArea.X += buttonAdjustment.X;
                    buttonArea.Y += buttonAdjustment.Y;
                    buttonArea.Height -= buttonAdjustment.Height;
                    buttonArea.Width -= buttonAdjustment.Width;


                    ButtonRenderer.DrawButton(graphics, buttonArea, System.Windows.Forms.VisualStyles.PushButtonState.Hot);

                    if (this.FormattedValue is String)
                    {
                        TextRenderer.DrawText(graphics, (string)this.FormattedValue, this.DataGridView.Font, buttonArea, Color.Brown);
                    }

                }
            }
        }
        #endregion
       
   
}

Add a Form on the project and design with multi button options to change the colors and fonts of the grid control.






and write the code given below
// Add a Simple Window form and design according to the given picture. Add buttons for the different options to change color and font
//Also add a fontDialog and ColorDialog box on this form (frmCustomize)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataGridViewApplication
{
    public partial class frmCustomize : Form
    {
        public frmCustomize()
        {
            InitializeComponent();
        }

        private ucDemoGrid _MyGrid;

        public ucDemoGrid MyGrid
        {
            get { return _MyGrid; }
            set { _MyGrid = value; }
        }

        private void btnHeaderForeground_Click(object sender, EventArgs e)  // On Click upon Set Header ForeColor
        {
            //Firstly set the [[ Enable Header Visual Style =False ]] 
            try
            {
                if (colorDialog1.ShowDialog() != DialogResult.Cancel)
                    _MyGrid.SetHeaderForeColor = colorDialog1.Color;
                _MyGrid.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnHeaderBackground_Click(object sender, EventArgs e)// On Click upon Set Header BackColor
        {
            try
            {
                if (colorDialog1.ShowDialog() != DialogResult.Cancel)
                    _MyGrid.SetHeaderBackColor = colorDialog1.Color;
                _MyGrid.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnCellForeground_Click(object sender, EventArgs e)// On Click upon Set Cell ForeColor
        {
            try
            {
                if (colorDialog1.ShowDialog() != DialogResult.Cancel)
                    _MyGrid.setDefaultCellStyleForeColor = colorDialog1.Color;
                _MyGrid.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnCellBackground_Click(object sender, EventArgs e)// On Click upon Set Cell BackColor
        {
            try
            {
                if (colorDialog1.ShowDialog() != DialogResult.Cancel)
                    _MyGrid.setDefaultCellStyleBackColor = colorDialog1.Color;
                    _MyGrid.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnGridLineColor_Click(object sender, EventArgs e)// On Click upon Set Grid Line Color
        {
            try
            {
                if (colorDialog1.ShowDialog() != DialogResult.Cancel)
                    _MyGrid.setGridLinesColor = colorDialog1.Color;
                _MyGrid.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnHeaderFont_Click(object sender, EventArgs e)// On Click upon Set Header Font
        {
            try
            {
                if (fontDialog1.ShowDialog() != DialogResult.Cancel)
                    _MyGrid.SetHeaderFont = fontDialog1.Font;
                _MyGrid.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnDefaultCellFont_Click(object sender, EventArgs e)// On Click upon Set Cell Font
        {
            try
            {
                if (fontDialog1.ShowDialog() != DialogResult.Cancel)
                    _MyGrid.setRowsDefaultCellStyleFontMarketWatch = fontDialog1.Font;
                _MyGrid.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnSelectionForeColor_Click(object sender, EventArgs e)// On Click upon Set Cell Selection Foreground Color
        {
            try
            {
                if (colorDialog1.ShowDialog() != DialogResult.Cancel)
                    _MyGrid.setSelectionForeColor= colorDialog1.Color;
                _MyGrid.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnSelectionBackColor_Click(object sender, EventArgs e)// On Click upon Set Cell Selection Background Color
        {
            try
            {
                if (colorDialog1.ShowDialog() != DialogResult.Cancel)
                    _MyGrid.setSelectionBackColor = colorDialog1.Color;
                _MyGrid.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnSetAlternateForeColor_Click(object sender, EventArgs e)// On Click upon Set Cell Alternate Foreground Color
        {
            try
            {
                if (colorDialog1.ShowDialog() != DialogResult.Cancel)
                    _MyGrid.setAlternateRowForeColor = colorDialog1.Color;
                _MyGrid.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnSetAlternateBgColor_Click(object sender, EventArgs e)// On Click upon Set Cell Alternate Background Color
        {
            try
            {
                if (colorDialog1.ShowDialog() != DialogResult.Cancel)
                    _MyGrid.setAlternateRowBackColor = colorDialog1.Color;
                _MyGrid.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnAlternateRowFont_Click(object sender, EventArgs e)// On Click upon Set Alternate Row Font
        {
            try
            {
                if (fontDialog1.ShowDialog() != DialogResult.Cancel)
                    _MyGrid.setAlternateFontMarketWatch = fontDialog1.Font;
                _MyGrid.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnClose_Click(object sender, EventArgs e)// close the property window
        {
            this.Hide();
        }
            
    }
}
Now attach that user control on the main form (Form1) and Run it

5 comments:

  1. Hi, Could you send the mail to me this version of the application's project?
    Thank you very much.
    tugay_galip@hotmail.com
    best regards

    ReplyDelete
  2. i can't do this project please can you attach file of project

    ReplyDelete
  3. can you send me a this project?
    airitaly@live.it

    ReplyDelete