Solved

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

Posted on 2012-04-09
4
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

759 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