Show MultiColumComboBox on DataGridView cell Click using C# and VB.Net in Windows Application

Last Reply 20 days ago By pandeyism

Posted 20 days ago

I want following code in desktop application:

The button highlighted in the below screenshot of multiColumComboBox in datagridviewrow should be visible only on mouse click event of the textbox else it should be hidden.

Can anybody help me on this?

You are viewing reply posted by: pandeyism 20 days ago.
Posted 20 days ago

Hi PK257,

Refer below sample.

Namespaces

C#

using System.Data;
using System.Drawing;

VB.Net

Imports System.Data
Imports System.Drawing

Code

C#

private void Form2_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Name");
    for (int j = 0; j < 20; j++)
    {
        dt.Rows.Add("A" + j.ToString());
    }
    this.dataGridView1.DataSource = dt;
    this.dataGridView1.Columns[0].Width = 200;
    this.txbtnControl = new TextAndComboControl();
    this.txbtnControl.Visible = false;
    this.dataGridView1.Controls.Add(this.txbtnControl);
    this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
    this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
}

TextAndComboControl txbtnControl;
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex == 0 && e.RowIndex > -1 && e.RowIndex != this.dataGridView1.NewRowIndex)
    {
        Rectangle textRect = e.CellBounds;
        textRect.Width -= e.CellBounds.Width / 3;
        Rectangle btnRect = e.CellBounds;
        btnRect.X += textRect.Width;
        btnRect.Width = e.CellBounds.Width / 3;
        e.Paint(textRect, DataGridViewPaintParts.All);
        ControlPaint.DrawButton(e.Graphics, btnRect, ButtonState.Normal);
        StringFormat formater = new StringFormat();
        formater.Alignment = StringAlignment.Center;
        e.Graphics.DrawString("click", e.CellStyle.Font, new SolidBrush(e.CellStyle.ForeColor), btnRect, formater);
        e.Handled = true;
    }
}

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0 && e.RowIndex > -1 && e.RowIndex != this.dataGridView1.NewRowIndex)
    {
        this.dataGridView1.CurrentCell.Value = this.txbtnControl.Text;
        this.txbtnControl.Visible = false;
    }
}

void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
    if (this.txbtnControl.Visible == true)
    {
        Rectangle r = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true);
        this.txbtnControl.Location = r.Location;
        this.txbtnControl.Size = r.Size;
    }
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0 && e.RowIndex > -1 && e.RowIndex != this.dataGridView1.NewRowIndex)
    {

        Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
        this.txbtnControl.Location = rect.Location;
        this.txbtnControl.Size = rect.Size;
        this.txbtnControl.Text = this.dataGridView1.CurrentCell.Value.ToString();
        this.txbtnControl.renderControl();
        this.txbtnControl.Visible = true;
    }
}
}

class TextAndComboControl : UserControl
{
private TextBox textbox1;
private ComboBox combobox1;
public TextAndComboControl()
{
    this.textbox1 = new TextBox();
    this.Controls.Add(this.textbox1);
    this.combobox1 = new ComboBox();
    this.Controls.Add(this.combobox1);
    this.renderControl();
}

public string Text
{
    get { return this.textbox1.Text; }
    set { this.textbox1.Text = value; }
}

public string ButtonText
{
    get { return this.combobox1.Text; }
    set { this.combobox1.Text = value; }
}

public void renderControl()
{
    this.textbox1.Location = new Point(0, 0);
    this.textbox1.Width = 2 * this.Width / 3;
    this.textbox1.Height = this.Height;
    this.combobox1.Location = new Point(2 * this.Width / 3, 0);
    this.combobox1.Width = this.Width / 3;
    this.combobox1.Height = this.Height;
}
}

VB.Net

Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim dt As DataTable = New DataTable()
        dt.Columns.Add("Name")
        For j As Integer = 0 To 20 - 1
            dt.Rows.Add("A" & j.ToString())
        Next
        Me.dataGridView1.DataSource = dt
        Me.dataGridView1.Columns(0).Width = 200
        Me.txbtnControl = New TextAndComboControl()
        Me.txbtnControl.Visible = False
        Me.dataGridView1.Controls.Add(Me.txbtnControl)
        Me.dataGridView1.CellEndEdit += New DataGridViewCellEventHandler(AddressOf dataGridView1_CellEndEdit)
        Me.dataGridView1.Scroll += New ScrollEventHandler(AddressOf dataGridView1_Scroll)
    End Sub

    Private txbtnControl As TextAndComboControl
    Private Sub dataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs)
        If e.ColumnIndex = 0 AndAlso e.RowIndex > -1 AndAlso e.RowIndex <> Me.dataGridView1.NewRowIndex Then
            Dim textRect As Rectangle = e.CellBounds
            textRect.Width -= e.CellBounds.Width / 3
            Dim btnRect As Rectangle = e.CellBounds
            btnRect.X += textRect.Width
            btnRect.Width = e.CellBounds.Width / 3
            e.Paint(textRect, DataGridViewPaintParts.All)
            ControlPaint.DrawButton(e.Graphics, btnRect, ButtonState.Normal)
            Dim formater As StringFormat = New StringFormat()
            formater.Alignment = StringAlignment.Center
            e.Graphics.DrawString("click", e.CellStyle.Font, New SolidBrush(e.CellStyle.ForeColor), btnRect, formater)
            e.Handled = True
        End If
    End Sub

    Private Sub dataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
        If e.ColumnIndex = 0 AndAlso e.RowIndex > -1 AndAlso e.RowIndex <> Me.dataGridView1.NewRowIndex Then
            Me.dataGridView1.CurrentCell.Value = Me.txbtnControl.Text
            Me.txbtnControl.Visible = False
        End If
    End Sub

    Private Sub dataGridView1_Scroll(ByVal sender As Object, ByVal e As ScrollEventArgs)
        If Me.txbtnControl.Visible = True Then
            Dim r As Rectangle = Me.dataGridView1.GetCellDisplayRectangle(Me.dataGridView1.CurrentCell.ColumnIndex, Me.dataGridView1.CurrentCell.RowIndex, True)
            Me.txbtnControl.Location = r.Location
            Me.txbtnControl.Size = r.Size
        End If
    End Sub

    Private Sub dataGridView1_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
        If e.ColumnIndex = 0 AndAlso e.RowIndex > -1 AndAlso e.RowIndex <> Me.dataGridView1.NewRowIndex Then
            Dim rect As Rectangle = Me.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
            Me.txbtnControl.Location = rect.Location
            Me.txbtnControl.Size = rect.Size
            Me.txbtnControl.Text = Me.dataGridView1.CurrentCell.Value.ToString()
            Me.txbtnControl.renderControl()
            Me.txbtnControl.Visible = True
        End If
    End Sub

Screenshot