ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Show MultiColumComboBox on DataGridView cell Click using C# and VB.Net in Windows Application<p>Hi <a class="username" rel="PK257"> PK257</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Drawing;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Drawing </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">private void Form2_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add(&#34;Name&#34;); for (int j = 0; j &lt; 20; j++) { dt.Rows.Add(&#34;A&#34; + 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 &amp;&amp; e.RowIndex &gt; -1 &amp;&amp; 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(&#34;click&#34;, e.CellStyle.Font, new SolidBrush(e.CellStyle.ForeColor), btnRect, formater); e.Handled = true; } } void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 0 &amp;&amp; e.RowIndex &gt; -1 &amp;&amp; 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 &amp;&amp; e.RowIndex &gt; -1 &amp;&amp; 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; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) Dim dt As DataTable = New DataTable() dt.Columns.Add(&#34;Name&#34;) For j As Integer = 0 To 20 - 1 dt.Rows.Add(&#34;A&#34; &amp; 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 &gt; -1 AndAlso e.RowIndex &lt;&gt; 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(&#34;click&#34;, 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 &gt; -1 AndAlso e.RowIndex &lt;&gt; 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 &gt; -1 AndAlso e.RowIndex &lt;&gt; 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</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/cm65DSn.gif" alt="" width="271" height="265" /></p>https://www.aspforums.net:443/Threads/504408/Show-MultiColumComboBox-on-DataGridView-cell-Click-using-C-and-VBNet-in-Windows-Application/https://www.aspforums.net:443/Threads/504408/Show-MultiColumComboBox-on-DataGridView-cell-Click-using-C-and-VBNet-in-Windows-Application/Mon, 29 Jul 2019 04:29:12 GMT