2

I want to have two controls in the same datagridview column.

I want to customize the DataGridViewComboBoxCell so that it will show the values of the selected value and on it a floating label with some text. in the past i was able to do it with a checkbox and a label but the problem with the DataGridViewComboBoxCell is that it comes out with an empty datasource when I override the paint event.

I tried to assign the datasource again after I used the Paint event but then although I see values in the DataGridViewComboBoxCell and the label showing the right value, I get into an infinite loop so I see the GUI blinking constantly.

10x for the help.

the code is the following:

*when the form loads

MyDGVCheckBoxColumn col = new MyDGVCheckBoxColumn();
        col.DataPropertyName = "value";
        col.DataSource = list;
        col.DisplayMember = "Yes";
        col.ValueMember = "value";

        col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        this.dataGridView1.Columns.Add(col);
        this.dataGridView1.RowCount = 50;

the class for the generic list:

public class CheckThis
{
    public string Yes { get; set; }
    public string value { get; set; }

    public CheckThis()
    {
        Yes = "gggg";
        value = "1";
    }
}

the code for the custom DataGridViewComboBoxCell (I used a similar example in the past from some site)

public class MyDGVCheckBoxColumn : DataGridViewComboBoxColumn
{
    private string label;

    public string Label
    {
        get
        {
            return label;
        }
        set
        {
            label = value;
        }
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return new MyDGVCheckBoxCell();
        }
    }
}

public class MyDGVCheckBoxCell : DataGridViewComboBoxCell
{
    private string label;

    public string Label
    {
        get
        {
            return label;
        }
        set
        {
            label = value;
        }

    }

    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 implementation paints the check box
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

        // Get the check box bounds: they are the content bounds
        Rectangle contentBounds = this.GetContentBounds(rowIndex);

        // Compute the location where we want to paint the string.
        Point stringLocation = new Point();
        stringLocation.Y = cellBounds.Y + 30;

        stringLocation.X = cellBounds.X + contentBounds.Bottom;


        // Paint the string.
        var res = false;
        MyDGVCheckBoxColumn col = (MyDGVCheckBoxColumn)this.OwningColumn;
        col.DataSource = list;
        col.DisplayMember = "Yes";
        col.ValueMember = "value";


                this.label = "Customer Does Not Appear";


        graphics.DrawString(
        this.Label, new Font("Arial", 6, FontStyle.Bold), System.Drawing.Brushes.Red, stringLocation);

    }


    public object list { get; set; }
}

0 Answers0