Solved

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

Posted on 2012-04-09
4
313 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:Murray Brown
[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:Murray Brown
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:Murray Brown
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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

615 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