How to make a Checkbox with text cell in a datagridview in a windows forms app?

Amr Nabiel 1 Reputation point
2020-12-24T23:47:13.85+00:00

I see a lot of windows forms commercial apps that I'm using like IDM or Advanced Renamer for example, use a custom made datagridview column which contains a checkbox with a picture and text. Just like any checkbox control in a windows form.

I've searched a lot about that, but I couldn't find anything about it, eve here links are outdated or with no direct answers.

Thanks in advance.

Please see attachment.51126-screenshot-2020-12-25-014402.png

Developer technologies Windows Forms
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Kyle Wang 5,531 Reputation points Microsoft External Staff
    2020-12-25T02:36:39.46+00:00

    You can try to re-paint the DataGridViewCheckBox. And use graphics.DrawString, graphics.DrawImage to draw the file name and file icon.

    Here is a simple demo.

    public class CustomCheckBoxColumn : DataGridViewCheckBoxColumn  
    {  
        private string label;  
      
        public string Label  
        {  
            get { return label; }  
            set { label = value; }  
        }  
      
        public override DataGridViewCell CellTemplate  
        {  
            get { return new CustomCheckBoxCell(); }  
        }  
    }  
      
    public class CustomCheckBoxCell : DataGridViewCheckBoxCell  
    {  
        // checkbox string   
        private string label;  
      
        public string Label  
        {  
            get { return label; }  
            set { label = value; }  
        }  
      
        // checkbox icon  
        private Image icon;  
      
        public Image Icon  
        {  
            get { return icon; }  
            set { icon = value; }  
        }  
      
        // override Paint  
        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 base Paint  
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);  
      
            // get the check box bounds  
            Rectangle contentBounds = this.GetContentBounds(rowIndex);  
      
            // the string location  
            Point stringLocation = new Point();  
            stringLocation.Y = cellBounds.Y + 2;  
            stringLocation.X = cellBounds.X + contentBounds.Right + 20;  
      
      
            // paint the string.  
            if (this.Label == null)  
            {  
                CustomCheckBoxColumn col = (CustomCheckBoxColumn)this.OwningColumn;  
                this.label = col.Label;  
            }  
      
            graphics.DrawString(  
            this.Label, Control.DefaultFont, System.Drawing.Brushes.Black, stringLocation);  
      
      
            // the icon rectangel  
            Rectangle rectangle = new Rectangle(cellBounds.X + 15, cellBounds.Y + 2, 15, 15);  
            // paint icon  
            graphics.DrawImage(icon, rectangle);  
        }  
    }  
    

    Then create the custom DataGridViewCheckBox in Form_Load.

    private void Form1_Load(object sender, EventArgs e)  
    {  
        CustomCheckBoxColumn col = new CustomCheckBoxColumn();  
        col.Width = 130;  
        col.Label = "this is a test string";  
        col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;  
        this.dataGridView1.Columns.Add(col);  
        this.dataGridView1.RowCount = 3;  
      
        foreach (DataGridViewRow row in this.dataGridView1.Rows)  
        {  
            ((CustomCheckBoxCell)row.Cells[0]).Label = "this is a test string";  
            ((CustomCheckBoxCell)row.Cells[0]).Icon = Image.FromFile(@"C:\Users\Administrator\Desktop\a.ico");  
            dataGridView1.Refresh();  
        }  
    }  
    

    Test result:

    51056-image.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.