Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > Components & Controls > Grids > Winforms Grids

Winforms Grids Discussion related to grid controls of all types for Winforms

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-06-2009, 11:22 AM
VB.NET Forum Enthusiast
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Sep 2008
Posts: 88
Reputation: 21
pettrer is on a distinguished programming path ahead
Default Changing to DataGridViewComboBoxCell

Hi all,

I'm trying to get a comboboxcell working in a datagridview. I DO NOT want a comboboxcolumn (that's what I have today) as the logic of my application states that only if the cell has a certain value when the form was opened, there should be a combobox instead of a regular textbox.

I've searched the net for a few hours and come up with the tiny piece of code below, but it doesn't work:

Code:
For i As Integer = 0 To dgvM.Rows.Count - nr
  If NullToStr(dgvM.Rows(i).Cells(COL_ANS).Value) = ans Then
    Dim foljerstr As String = NullToStr(dgvM.Rows(i).Cells(COL_FOLJER).Value)
    Dim combocell As DataGridViewComboBoxCell = dgvM.Rows(i).Cells(COL_FOLJER)
    combocell.Items.Add("YES")
    combocell.Items.Add("NO")
    dgvM.Rows(i).Cells(COL_FOLJER).Value = foljerstr
  End If
Next
The "Dim combocell..." line gives the error: "Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewComboBoxCell'."

While I can understand the error message in itself and why it appears, this seems to be the manner in which other people have solved this problem. I'm puzzled!

Warm regards,

Pettrer
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-06-2009, 4:30 PM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,173
Reputation: 1273
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

Quote:
Dim combocell As DataGridViewComboBoxCell = dgvM.Rows(i).Cells(COL_FOLJER)
The error message says that Cells(COL_FOLJER) is a DataGridViewTextBoxCell, not a DataGridViewComboBoxCell.

You haven't changed cell type in your code anywhere, this is how:
Code:
Dim cell As New DataGridViewComboBoxCell
cell.Items.Add("A")
cell.Items.Add("B")
Me.DataGridView1.Item(col, row) = cell
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-07-2009, 4:32 AM
VB.NET Forum Enthusiast
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Sep 2008
Posts: 88
Reputation: 21
pettrer is on a distinguished programming path ahead
Default

Hi John,

Thank you so much! It worked like a charm!

/Pettrer
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 1:06 PM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.