Solved

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

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Want to create a userform that looks like the image provided 10 63
Get hold of longitude and latitude in iframe string 11 46
vb.net help 4 31
Store results in vb.net 3 20
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 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