Solved

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

Posted on 2012-04-09
4
294 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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 …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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