Question Painting the cell background of a custom DataGridView column

nK0de

New member
Joined
Jan 20, 2012
Messages
1
Location
Colombo, Sri Lanka
Programming Experience
1-3
I'm trying to create a DataGridView column hosted with Radiobuttons. I've been following this MSDN article. I've uploaded the Visual Studio project here. And for those who are unsure about downloading unknown attachments, here's the code I've written so far.

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;


namespace Radiobutton_Column
{
    //RadiobuttonColumn class
    class RadiobuttonColumn : DataGridViewColumn
    {
        public RadiobuttonColumn()
            : base(new RadiobuttonCell())
        {
        }


        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                if (value != null && !value.GetType().IsAssignableFrom(typeof(RadiobuttonCell)))
                {
                    throw new InvalidCastException("Must be a RadiobuttonCell");
                }
            }
        }
    }


    //RadiobuttonCell class
    public class RadiobuttonCell : DataGridViewCell
    {
        public RadiobuttonCell()
            : base()
        {
        }


        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            RadiobuttonEditingControl rdb = DataGridView.EditingControl as RadiobuttonEditingControl;
            if (this.Value == null)
            {
                rdb.Checked = false;
            }
            else
            {
                rdb.Checked = (Boolean)this.Value;
            }
        }


        public override Type EditType
        {
            get
            {
                return typeof(RadiobuttonEditingControl);
            }
        }


        public override Type ValueType
        {
            get
            {
                return typeof(Boolean);
            }
        }


        public override object DefaultNewRowValue
        {
            get
            {
                return false;
            }
        }


        //painting the radiobutton
        protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.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, advancedBorderStyle, paintParts);




            Rectangle rectRadioButton = default(Rectangle);


            rectRadioButton.Width = 14;
            rectRadioButton.Height = 14;
            rectRadioButton.X = cellBounds.X + (cellBounds.Width - rectRadioButton.Width) / 2;
            rectRadioButton.Y = cellBounds.Y + (cellBounds.Height - rectRadioButton.Height) / 2;


            if (Convert.IsDBNull(value) || Convert.ToBoolean(value) == false)
            {
                ControlPaint.DrawRadioButton(graphics, rectRadioButton, ButtonState.Normal);
            }
            else
            {
                ControlPaint.DrawRadioButton(graphics, rectRadioButton, ButtonState.Checked);
            }
        }
    }


    //RadiobuttonEditingControl class
    class RadiobuttonEditingControl : RadioButton, IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;


        public RadiobuttonEditingControl()
        {
            this.Checked = false;
        }


        public object EditingControlFormattedValue
        {
            get
            {
                return this.Checked = true;
            }
            set
            {
                this.Checked = false;
            }
        }


        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }


        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
        }


        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }


        public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
        {
            switch (key & Keys.KeyCode)
            {
                case Keys.Space:
                    return true;
                default:
                    return !dataGridViewWantsInputKey;
            }
        }


        public void PrepareEditingControlForEdit(bool selectAll)
        {
        }


        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }


        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }


        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }


        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }


        protected override void OnCheckedChanged(EventArgs eventArgs)
        {
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.Checked = false;
        }
    }
}


When I run it, no errors occur. But the background of the cells appear transparent. Like this. I'm a bit confused on how to paint the background of cells, what events and methods to use etc.

If anyone could take a look at this and point me to the right direction as to what changes should be done in the code, I'd be grateful.

And also this is my first attempt at creating something like this. Although I followed the example code in the MSDN article, I've not completely grasped what every bit of code in it does. I'm still working on it. So I may have used unwanted pieces of code in mine. If you can spot any unnecessary code pieces in my code, please let me know.

Thank you very much.
 
Default DataGridViewCell.Paint draws nothing, only the derived cell types has default painting, use Graphics.FillRectangle method to draw the background.
PaintBorder method is available to you however.
 

Latest posts

Back
Top Bottom