Hi,
I am develop an windows application of OMR in c#. I just want to add rediobutton instead of check box. how i can do this.

Anybody help me.....

Thanks in advance

You would have to add a DataGridViewRadioButtonColumn, either in designmode or at runtime. Use the CustomDataGridView instead of the DataGridView when you design the form.

public class DataGridViewRadioButtonColumn : DataGridViewTextBoxColumn  
    {  
        internal const int radioButtonSize = 14;  
 
        public DataGridViewRadioButtonColumn()  
            :base()  
        {  
            this.ReadOnly = true;  
        }  
 
        internal static void Paint(Graphics g, Rectangle cellBounds, bool state)  
        {  
            cellBounds.Inflate(-1, -1);  
            Brush drawBrsh = new SolidBrush(SystemColors.Control);  
            g.FillRectangle(drawBrsh, cellBounds);  
            cellBounds.Inflate(0, -((cellBounds.Height - radioButtonSize) / 2));  
            ControlPaint.DrawRadioButton(g, cellBounds, (state) ? ButtonState.Checked : ButtonState.Normal);  
            drawBrsh.Dispose();  
        }  
    }  
 
    public class CustomDataGridView : DataGridView  
    {  
        protected override void OnCellClick(DataGridViewCellEventArgs e)  
        {  
            if (e.RowIndex > -1)  
            {  
                DataGridViewCell cell = this[e.ColumnIndex, e.RowIndex];  
                if (cell.OwningColumn is DataGridViewRadioButtonColumn)  
                {  
                    if (cell.FormattedValue.ToString().Length == 0)  
                    {  
                        for (int i = 0; i < this.RowCount; i++)  
                        {  
                            this[e.ColumnIndex, i].Value = string.Empty;  
                        }  
                        cell.Value = "Selected";  
                    }  
                }  
            }  
            base.OnCellClick(e);  
        }  
 
        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)  
        {  
            if ((e.RowIndex > -1) && (e.ColumnIndex > -1) &&  
                (this.Columns[e.ColumnIndex] is DataGridViewRadioButtonColumn))  
            {  
                e.PaintBackground(e.CellBounds, true);  
                DataGridViewRadioButtonColumn.Paint(e.Graphics, e.CellBounds,  
                    (e.FormattedValue.ToString().Length > 0));  
                e.Handled = true;  
            }  
 
            base.OnCellPainting(e);  
        }  
    }

hi, Mitja Bonca
Thanks for reply. i am used this code by creating the class file and also component file. the control is added to the datagridview but when i am running it but its look like simple textbox in datagridview.

what is mean by custom Datagridview.
still not getting....

Edited 5 Years Ago by bhagawatshinde: n/a

This article has been dead for over six months. Start a new discussion instead.