Question multi-field Combobox in DataGridView ?

accmaster

New member
Joined
Mar 1, 2013
Messages
1
Programming Experience
1-3
Hi there,

have a look on attached image, there is a combo box inside datagridview with field name "NEHA NO", when click on it a list appears below combo box with 4 different fields, on change event of combo box list also changes. My client want me to design same type of grid with same type of combo box. please help me out and guide me what is this?

Is this a multi-field combo box ? if yes how to design it in datagridview?
Is this a list view below combo box? how to call list view below combo box?

I hope all of you will try your level best to pull me out from this problem and provide me some solution.

Thanks in advance
Ladak

ImportInquiry_2.JPG
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,457
Location
Sydney, Australia
Programming Experience
10+
The standard .NET ComboBox does not support multiple columns in the drop-down list but, if you look online, you'll find a number of examples of controls that inherit ComboBox and then add that behaviour. Here's one that was the first result when Googling vb.net multi column combobox:

Multiple columns combo box vb.net

Just note that there may be more and better examples that are written in C# and there's nothing to stop you using those. In that case you could convert the code to VB or simply compile the C# code into a DLL and then reference that in your existing project, which is exactly the case with all the standard .NET controls already.

Once you've got a regular control that does what you want, you can create your own custom column, cell and editing control types to allow you to embed that control in a DataGridView. The MSDN Library provides an example of embedding a DateTimePicker in a DataGridView and you can follow a similar principle to embed any control you want.

How to: Host Controls in Windows Forms DataGridView Cells
 

Retelist

Member
Joined
Apr 7, 2013
Messages
7
Programming Experience
Beginner
I have the code below, and I need to make a DataGridView multi-column combobox from it:

VB.NET:
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Public Class MainForm
        Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                Dim dtTest As DataTable = New DataTable()
                dtTest.Columns.Add("ID", GetType(Integer))
                dtTest.Columns.Add("Name", GetType(String))
                dtTest.Columns.Add("Surname", GetType(String))

                dtTest.Rows.Add(1, "John", "Wolf")
                dtTest.Rows.Add(2, "Amy", "Blue")
                dtTest.Rows.Add(3, "Tony", "Jhonson")
                dtTest.Rows.Add(4, "Bruce", "Lee")
                dtTest.Rows.Add(5, "Allen", "deWatt")
                ' Bind the ComboBox to the DataTable
                Me.comboBox1.DataSource = dtTest
                Me.comboBox1.DisplayMember = "Name"
                Me.comboBox1.ValueMember = "ID"
                ' Enable the owner draw on the ComboBox.
                Me.comboBox1.DrawMode = DrawMode.OwnerDrawFixed
                ' Handle the DrawItem event to draw the items.
        End Sub
        Private Sub comboBox1_DrawItem(ByVal sender As System.Object, _
                                                                   ByVal e As System.Windows.Forms.DrawItemEventArgs) _
                                                                   Handles comboBox1.DrawItem
                ' Draw the default background
                e.DrawBackground()
                ' The ComboBox is bound to a DataTable,
                ' so the items are DataRowView objects.
                Dim drv As DataRowView = CType(comboBox1.Items(e.Index), DataRowView)
                ' Retrieve the value of each column.
                Dim id As Integer = drv("ID").ToString()
                Dim nume As String = drv("Name").ToString()
                Dim prenume As String = drv("Surname").ToString()
                ' Get the bounds for the first column
                Dim r1 As Rectangle = e.Bounds
                r1.Width = r1.Width / 6
                ' Draw the text on the first column
                Using sb As SolidBrush = New SolidBrush(e.ForeColor)
                        e.Graphics.DrawString(id, e.Font, sb, r1)
                End Using
                ' Draw a line to isolate the columns
                Using p1 As Pen = New Pen(Color.Black)
                        e.Graphics.DrawLine(p1, r1.Right, 0, r1.Right, r1.Bottom)
                End Using
                ' Get the bounds for the second column
                Dim r2 As Rectangle = e.Bounds
                r2.X = e.Bounds.Width / 1.5
                r2.Width = r2.Width / 5
                ' Draw the text on the second column
                Using sb As SolidBrush = New SolidBrush(e.ForeColor)
                        e.Graphics.DrawString(name, e.Font, sb, r2)
                End Using

                ' Get the bounds for the tirdt column
                Dim r3 As Rectangle = e.Bounds
                r3.X = e.Bounds.Width / 6
                r3.Width = r3.Width / 2
                ' Draw the text on the second column
                Using sb As SolidBrush = New SolidBrush(e.ForeColor)
                        e.Graphics.DrawString(surname, e.Font, sb, r3)
                End Using
                Using p2 As Pen = New Pen(Color.Black)
                        e.Graphics.DrawLine(p2, r2.Left, 0, r2.Left, r2.Bottom)
                End Using
        End Sub
End Class

I also try to understand this:
How to: Host Controls in Windows Forms DataGridView Cells

No success yet. Please help :)
 

kvinvisibleguy

New member
Joined
Sep 22, 2015
Messages
1
Location
Bangalore,India
Programming Experience
5-10
VB.NET:
''--------------------------------------------------------Start
Imports System:Imports System.ComponentModel
Imports System.Data:Imports System.Globalization
Imports System.Diagnostics:Imports System.Web:
Imports System.IO:Imports System.Attribute
Imports System.CodeDom:Imports System.Net.WebClient
Imports System.Drawing:Imports Microsoft.VisualBasic
Imports Microsoft.Win32:Imports System.Net
Imports System.Object:Imports System.Xml
Imports System.Reflection:Imports System.Collections
Imports System.Runtime.InteropServices
Imports System.Windows:Imports System.Windows.Forms
Imports System.Threading:Imports System.CodeDom.Compiler
Imports System.Collections.Generic:Imports System.Text
Imports System.Runtime:Imports System.Drawing.Drawing2D
Imports System.Runtime.Serialization

Namespace Book4

''''''''''''''''''''''''''''''Style_Class''''''''''''''''''''''''''''
Public Class MyButton
    Inherits Button

    Public Sub New()
        Me.TextAlign = ContentAlignment.MiddleCenter
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       MyBase.OnPaint(e)
       Dim myEllipse As New System.Drawing.Rectangle(0, 0,Me.Width,Me.Height)
       Dim myPath As New GraphicsPath
       myPath.AddEllipse(myEllipse)
       Me.Region = New Region(myPath)

     For i As Integer =0 To -30 Step -3
       Dim borderRectangle As Rectangle = Me.ClientRectangle
       borderRectangle.Inflate(i,i)
       ControlPaint.DrawBorder3D(e.Graphics, borderRectangle, _
                                        Border3DStyle.Raised)
     Next

           ControlPaint.DrawBorder(e.Graphics, ClientRectangle, _
                                   Color.Blue,8, _
                                   ButtonBorderStyle.Inset,Color.Blue, _
                                   8,ButtonBorderStyle.Inset, _
                                   Color.Blue,8, _
                                   ButtonBorderStyle.Inset, Color.Blue, _
                                   8, ButtonBorderStyle.Inset)

     End Sub

End Class
''''''''''''''''''''''''''''''Style_Class''''''''''''''''''''''''''''
''Controls_Style

Public Class Styled_ComboBox1
     Inherits System.Windows.Forms.Form
''-------------------------------------------------------------

''-------------------------------------------------------------
Public ComboBox1 As ComboBox
Public Button1 As Button
Public TextBox1 As TextBox
Public TextBox2 As TextBox
Public TextBox3 As TextBox
Public TextBox4 As TextBox
Public arr1 As String()
Public arr2 As String()

    Public Sub New()
''-------------------------------------------------------------

''-------------------------------------------------------------
   Me.SuspendLayout()
''-------------------------------------------------------------tbx1.Text
   ComboBox1 = New ComboBox
   ComboBox1.Size = New System.Drawing.Size(300,90)
   ComboBox1.Location =New System.Drawing.Point(50,150)
   ComboBox1.BackColor=System.Drawing.Color.Khaki
   ComboBox1.ForeColor= System.Drawing.Color.Indigo
   ComboBox1.DropDownWidth = 1300
   ComboBox1.DropDownHeight = 800
   ComboBox1.ItemHeight = 30
   ComboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable
   ComboBox1.Font = New System.Drawing.Font("Slab Serif",20.0!, _
   System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point, _
                        CType(0, Byte))
''-------------------------DataSource--------------------------
     For i = 0 To arr1.Length -2
       listB.AddRange(New String() {arr1(i) & arr1(i)})                                 
       ComboBox1.DataSource = listB
       Threading.Thread.Sleep(0.1)
       listB.AddRange(New String() {arr2(i) & arr2(i)})                                 
       ComboBox1.DataSource = listB     
     Next
       listB.AddRange(New String() {strZ & strZ})
   Me.Controls.Add(ComboBox1)
''-------------------------------------------------------------
        AddHandler ComboBox1.DrawItem , New _
        DrawItemEventHandler(AddressOf ComboBox1_DrawItem)

   Button1 = New MyButton
   Button1.Size = New System.Drawing.Size(200,120)
   Button1.Location =New System.Drawing.Point(5,5)
   Button1.BackColor=System.Drawing.Color.RoyalBlue
   Button1.ForeColor= System.Drawing.Color.FloralWhite
   Button1.Text = "Button1"
   Button1.Font = New System.Drawing.Font("Algerian",15.0!, _
   System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point, _
                        CType(0, Byte))

   TextBox1 = New TextBox
   TextBox1.Size = New System.Drawing.Size(200,100)
   TextBox1.Location =New System.Drawing.Point(300,5)
   TextBox1.BackColor=System.Drawing.Color.Black
   TextBox1.ForeColor= System.Drawing.Color.White
   TextBox1.Text = "TextBox1"
   TextBox1.Font = New System.Drawing.Font("Segoe UI",14.0!, _
   System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point, _
                        CType(0, Byte))

   TextBox2 = New TextBox
   TextBox2.Size = New System.Drawing.Size(200,100)
   TextBox2.Location =New System.Drawing.Point(510,5)
   TextBox2.BackColor=System.Drawing.Color.Black
   TextBox2.ForeColor= System.Drawing.Color.White
   TextBox2.Text = "TextBox2"
   TextBox2.Font = New System.Drawing.Font("Segoe UI",14.0!, _
   System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point, _
                        CType(0, Byte))

   TextBox3 = New TextBox
   TextBox3.Size = New System.Drawing.Size(200,100)
   TextBox3.Location =New System.Drawing.Point(720,5)
   TextBox3.BackColor=System.Drawing.Color.Black
   TextBox3.ForeColor= System.Drawing.Color.White
   TextBox3.Text = "TextBox3"
   TextBox3.Font = New System.Drawing.Font("Segoe UI",14.0!, _
   System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point, _
                        CType(0, Byte))

   TextBox4 = New TextBox
   TextBox4.Size = New System.Drawing.Size(200,100)
   TextBox4.Location =New System.Drawing.Point(930,5)
   TextBox4.BackColor=System.Drawing.Color.Black
   TextBox4.ForeColor= System.Drawing.Color.White
   TextBox4.Text = "TextBox4"
   TextBox4.Font = New System.Drawing.Font("Segoe UI",14.0!, _
   System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point, _
                        CType(0, Byte))

''Control_Property
''-------------------------------------------------------------
''Specific_Insert_Code
''-------------------------------------------------------------
   Me.Controls.Add(ComboBox1)
   Me.Controls.Add(Button1)
   Me.Controls.Add(TextBox1)
   Me.Controls.Add(TextBox2)
   Me.Controls.Add(TextBox3)
   Me.Controls.Add(TextBox4)
''Controls_Inclusion
''-------------------------------------------------------------

Me.ClientSize = New System.Drawing.Size(1920,1040)
Me.BackColor = ColorTranslator.FromHtml("#FF2F4F4F")
''Me.BackgroundImage = Image.FromFile("D:\DllMaster\BG\BG_Image12.jpg")
Me.Text ="Styled_ComboBox1"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.Name = "Styled_ComboBox1"
Me.AutoScroll = True
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub

''Specific_Insert_Events
''-------------------------------------------------------------
    Public Shared Sub Main()
       Dim form1 As Styled_ComboBox1 = New Styled_ComboBox1()
       form1.ShowDialog()
    End Sub
Public Sub Styled_ComboBox1_Loaded(sender As Object, e As EventArgs) _
                           Handles MyBase.Load
   Try
      Try
                Dim dtTest As DataTable = New DataTable()
                dtTest.Columns.Add("ID", GetType(Integer))
                dtTest.Columns.Add("Name", GetType(String))
                dtTest.Columns.Add("Surname", GetType(String))

                dtTest.Rows.Add(1, "John", "Wolf")
                dtTest.Rows.Add(2, "Amy", "Blue")
                dtTest.Rows.Add(3, "Tony", "Jhonson")
                dtTest.Rows.Add(4, "Bruce", "Lee")
                dtTest.Rows.Add(5, "Allen", "deWatt")             
                ComboBox1.DataSource = dtTest
                ComboBox1.DisplayMember = "Name"
                ComboBox1.ValueMember = "ID"
            
      Catch ex As Exception
         System.Windows.Forms.MessageBox.Show(ex.ToString)
      End Try
   Catch ex As Exception
      System.Windows.Forms.MessageBox.Show(ex.ToString)
   End Try
End Sub

    Public Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e _
                          As System.Windows.Forms.DrawItemEventArgs)
     Try
       Try       
         e.DrawBackground()     
         Dim drv As DataRowView = CType(comboBox1.Items(e.Index), DataRowView)             
         Dim id As Integer = drv("ID").ToString()
         Dim nume As String = drv("Name").ToString()
         Dim prenume As String = drv("Surname").ToString()     
         Dim r1 As Rectangle = e.Bounds
         r1.Height = r1.Width / 6     
            Using sb As SolidBrush = New SolidBrush(System.Drawing.Color.Brown)  '''
               e.Graphics.DrawString(id, e.Font, sb, r1)
            End Using         
               Using p1 As Pen = New Pen(Color.Black)
                  e.Graphics.DrawLine(p1, r1.Right, 0, r1.Right, r1.Bottom)
               End Using           
                  Dim r2 As Rectangle = e.Bounds
                  r2.X = e.Bounds.Width / 1.5
                  r2.Width = r2.Width / 5
            
                Using sb As SolidBrush = New SolidBrush(System.Drawing.Color.Green)
                   e.Graphics.DrawString(nume, e.Font, sb, r2)
                End Using
            
                Dim r3 As Rectangle = e.Bounds
                r3.X = e.Bounds.Width / 6
                r3.Width = r3.Width / 2
            
                Using sb As SolidBrush = New SolidBrush(e.ForeColor)
                   e.Graphics.DrawString(prenume, e.Font, sb, r3)
                End Using

                Using p2 As Pen = New Pen(Color.Black)
                   e.Graphics.DrawLine(p2, r2.Left, 0, r2.Left, r2.Bottom)
                End Using
       Catch ex As Exception
          System.Windows.Forms.MessageBox.Show(ex.ToString)
       End Try
     Catch ex As Exception
       System.Windows.Forms.MessageBox.Show(ex.ToString)
     End Try
    End Sub
''-------------------------------------------------------------

    Public Sub Button1_Click(ByVal sender As Object, _
                                        ByVal e As System.EventArgs)
    Try

       ComboBox1.SelectedIndex = 9

    Catch ex As Exception
       System.Windows.Forms.MessageBox.Show(ex.ToString)
    End Try
    End Sub

''Event_Handlers

    End Class
End Namespace
 
Last edited by a moderator:
Top Bottom