ARTICLE

Add and Fill DataGridViewComboBoxColumn in VB.NET

Posted by Shahan Ayyub Articles | ADO.NET in VB.NET January 15, 2011
This will demonstrate how you can bind DataGridViewComboBoxColumn to your DataGridView and populate it from a database.
 
Reader Level:


This will demonstrate how you can bind DataGridViewComboBoxColumn to your DataGridView and populate it from a database.

The way(s) you can follow are:

1) Create and add a DataGridViewComboBoxColumn in your datagridview and populate every combo Box with the values in an iterative manner.


2) Create a DataGridViewComboBoxColumn in your datagridview, take a Generic List, fill it with item(s) and Bind to the ComboBox's DataSource as mention in the below code:


See the code, here Db2DataSet is filled before:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       'Bind Dataset that don't have comboboxcolumn you are willing to add
        DataGridView1.DataSource = Db2DataSet.Tables(0)
        'Create a comboboxcolumn for Datagridview
        DataGridView1.Columns.Add(New DataGridViewComboBoxColumn() With {.HeaderText = "ComboColumn", .Name = "ComboColumn"})
        Dim Ilist As New List(Of String) 'A list that will hold combobox items
        Ilist.AddRange(New String() {"item1", "item2", "item3", "item4", "item5"}) ' Items that are required in combobox
        DirectCast(DataGridView1.Columns("ComboColumn"), DataGridViewComboBoxColumn).DataSource = Ilist  'Bind to item to "ComboColumn"
    End
Sub


Login to add your contents and source code to this article
share this article :
post comment
 

Thank you

Posted by Shahan Ayyub Feb 16, 2011

Very nice article.

Posted by Sapna Feb 01, 2011
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor