none
How to change size of combobox/Checkbox in DataGridViewComboBoxCell/DataGridViewCheckBoxCell RRS feed

Answers

  • Hello SUNIL,

    To resize a DataGridViewCombobBoxCell or DataGridViewCheckBoxCell, you can override the Paint() method, the demo comes:

               public class DataGridViewDisableCheckBoxCell : DataGridViewCheckBoxCell
                {
                    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()
                    {
                        DataGridViewDisableCheckBoxCell cell = (DataGridViewDisableCheckBoxCell)base.Clone();
                        cell.Enabled = this.Enabled;
                        return cell;
                    }
                    // By default, enable the button cell.
                    public DataGridViewDisableCheckBoxCell()
                    {
                        this.enabledValue = true;
                    }


                    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)
                    {
                        if (!this.enabledValue)
                        {
                            // Draw the cell background, if specified.
                            if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
                            {
                                SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);
                                graphics.FillRectangle(cellBackground, cellBounds);
                                cellBackground.Dispose();
                            }

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

                            // Paint background
                            //graphics.FillRectangle(Brushes.Silver, cellBounds);
                             //draw a inactive checkbox
                            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
                        }
                        else
                        {
                            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
                        }
                    }
                } 

    Here is a thread dedicated in auto-sizing the DataGridViewComboBoxColumn in respects of its the contents of item with maximum length in Items Collection which may give you other angle of the question: Auto Size the DataGridViewComboBoxColumn Drop Down to the contents.

    Just a note that once the height of the DataGridView less than 17, the DataGridViewComboBoxCell/DataGridViewCheckBoxCell will disappear since the heigh is too small to show a comboBox or Checkbox.

    Hope this helps. If you have any questions, please feel free to let me know.

    Sincerely,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Monday, August 30, 2010 6:55 AM
    Wednesday, August 25, 2010 9:43 AM

All replies

  • Can any one give the solution for resizing the combobox. after lot of searching i came to know that we can achive it only through Cellpainting. can any one give the approach to reszie the combobox using paint method
    Thursday, August 19, 2010 4:37 PM
  • Hello SUNIL,

    To resize a DataGridViewCombobBoxCell or DataGridViewCheckBoxCell, you can override the Paint() method, the demo comes:

               public class DataGridViewDisableCheckBoxCell : DataGridViewCheckBoxCell
                {
                    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()
                    {
                        DataGridViewDisableCheckBoxCell cell = (DataGridViewDisableCheckBoxCell)base.Clone();
                        cell.Enabled = this.Enabled;
                        return cell;
                    }
                    // By default, enable the button cell.
                    public DataGridViewDisableCheckBoxCell()
                    {
                        this.enabledValue = true;
                    }


                    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)
                    {
                        if (!this.enabledValue)
                        {
                            // Draw the cell background, if specified.
                            if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
                            {
                                SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);
                                graphics.FillRectangle(cellBackground, cellBounds);
                                cellBackground.Dispose();
                            }

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

                            // Paint background
                            //graphics.FillRectangle(Brushes.Silver, cellBounds);
                             //draw a inactive checkbox
                            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
                        }
                        else
                        {
                            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
                        }
                    }
                } 

    Here is a thread dedicated in auto-sizing the DataGridViewComboBoxColumn in respects of its the contents of item with maximum length in Items Collection which may give you other angle of the question: Auto Size the DataGridViewComboBoxColumn Drop Down to the contents.

    Just a note that once the height of the DataGridView less than 17, the DataGridViewComboBoxCell/DataGridViewCheckBoxCell will disappear since the heigh is too small to show a comboBox or Checkbox.

    Hope this helps. If you have any questions, please feel free to let me know.

    Sincerely,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Monday, August 30, 2010 6:55 AM
    Wednesday, August 25, 2010 9:43 AM