Solved

VB.net adding ComboBox columns to a DataGridView that can accept text

Posted on 2012-04-09
4
277 Views
Last Modified: 2012-04-09
Hi. I am using the following loop to add over two hundred columns to a DataGridView.
I need to programatically set the text value of any of the cells to something but get an error when I do this. How can I change my code so that text can be dropped in too?          


  Dim xx As New DataGridViewComboBoxColumn

        For i as integer = 0 to UBound(arrHeaders)

            xx.Name = arrHeaders(i)

            xx.Items.Add("Yes")

            xx.Items.Add("No")

            Me.DataGridView1.Columns.Add(xx)


        Next I
0
Comment
Question by:murbro
  • 2
  • 2
4 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 37824104
Simply do:
For i As Integer = 0 To UBound(arrHeaders)
    Me.DataGridView1.Columns.Add(New DataGridViewComboBoxColumn With {.DataSource = {"Yes", "No"}, .Name = arrHeaders(i)})
Next i

Open in new window

0
 

Author Comment

by:murbro
ID: 37824123
Hi. Thanks, but if I try and change the text to something that isn't in the list of ComboBox items I get an error
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 37824158
Ahhh! you need to hande the CellValidating event of your datagridview to add thenew item. Add the following code (assuming you datagridviewis called DataGridView1)
If DataGridView1.CurrentCell.IsInEditMode Then
    If TypeOf (DataGridView1.CurrentCell) Is DataGridViewComboBoxCell Then
        If String.IsNullOrEmpty(e.FormattedValue.ToString) Then Exit Sub
        Dim cb = DirectCast(DataGridView1.CurrentCell, DataGridViewComboBoxCell)
        If Not cb.Items.Contains(e.FormattedValue) Then
            cb.Items.AddRange(e.FormattedValue)
            cb.Value = e.FormattedValue
        End If
    End If
End If

Open in new window

NOTE: I additionally assume that you have already enabled editing on your datagridviewcomboboxcell!)
0
 

Author Closing Comment

by:murbro
ID: 37824204
fantastic. Thanks very much
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now