Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

system.argumentexception:DataGridViewcombobox cell value is not valid

Posted on 2009-04-05
17
Medium Priority
?
3,898 Views
Last Modified: 2012-05-06
i am trying to assign the value of a particular cell from the respective column in the database ...here is the code i tried.....

 For Each row As DataGridViewRow In Me.DataGridView1.Rows
           
            Dim CTRcell As DataGridViewComboBoxCell = DirectCast(row.Cells(0), DataGridViewComboBoxCell)
            CTRcell.Value = centeridandname
           
        Next

i am getting an exception system.argumentexception:DataGridViewcombobox cell value is not valid.

Please find the exception details attached.
Untitled.jpg
0
Comment
Question by:yuvaratna
[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
  • 9
  • 8
17 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 24072895
Do you have the combobox in the 1st column ?
Try to change to :

For Each row As DataGridViewRow In Me.DataGridView1.Rows
        If not row.IsNewRow Then
            Dim CTRcell As DataGridViewComboBoxCell = DirectCast(row.Cells(0), DataGridViewComboBoxCell)
            CTRcell.Value = centeridandname
        End If
  Next

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24072955
Sorry wrong answer :(
The problem is not the new row, for that message, but the assing of the value. If you want to select one value you can do this way:

        For Each row As DataGridViewRow In Me.DataGridView1.Rows
            If Not row.IsNewRow Then
                row.Cells(0).Value = "1"
            End If
        Next
 
        ' Where "1" is the name of the item to select 

Open in new window

0
 

Author Comment

by:yuvaratna
ID: 24073008
Hi Ipaulino,I was waiting for you, Thanks for your reply, i tried the above code...but  "row.IsNewRow" is returning true....if i commented the if not row is new row line, it is returning the same exception "system.argumentexception:DataGridViewcombobox cell value is not valid."
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 48

Expert Comment

by:jpaulino
ID: 24073018
Check my second comment :)
I have understood wrongly your problem.
0
 

Author Comment

by:yuvaratna
ID: 24073044
heer is the code i have

For Each row As DataGridViewRow In Me.DataGridView1.Rows
           
             
                row.Cells(0).Value =centeridandname
         
        Next

in this case it is returning the same exception....


0
 

Author Comment

by:yuvaratna
ID: 24073051
when i tried


 For Each row As DataGridViewRow In Me.DataGridView1.Rows
            If Not row.IsNewRow Then
             
                row.Cells(0).Value = ObjTA.centeridandname
            End If
        Next


The execution is not going to the row.cell(0).value line because, the "row.isnewrow" is returning true...
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24073056
What is centeridandname variable ? You must specify a valid STRING that are in the itens of the combobox
You could try
row.Cells(0).Value =centeridandname.ToString()
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24073067
Ok, it's a new row then remove that line.
0
 

Author Comment

by:yuvaratna
ID: 24073083
nope! that is not working out!.. it is returning the same error
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24073089
Can you show a picture of what you have ?
centeridandname is a string variable and exists in the datagridviewcombobox ?
0
 

Author Comment

by:yuvaratna
ID: 24073130
the canterid and name is returning "0000-abc" , which exists in the combobox dataset...
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 2000 total points
ID: 24073317
Humm ok, you have a datasource for the combobox!
You can still use the code I have showed you but you need to cancel the error.  On the DataError event you can clear the error this way:
(this was documented as a datagridview problem in VS2005 ... didn't knew that still exists in VS2008)

    Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        If e.Context = DataGridViewDataErrorContexts.Formatting Or e.Context = DataGridViewDataErrorContexts.PreferredSize Then
            e.ThrowException = False
        End If
    End Sub

Open in new window

0
 

Author Comment

by:yuvaratna
ID: 24073331
k ipaulo, thanks a lot, Give a min, i l chek on this....
0
 

Author Comment

by:yuvaratna
ID: 24073352
i have this right now

For Each row As DataGridViewRow In Me.DataGridView1.Rows
         
            row.Cells(0).Value = ObjTA.centeridandname.ToString()
           
        Next

 Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        If e.Context = DataGridViewDataErrorContexts.Formatting Or e.Context = DataGridViewDataErrorContexts.PreferredSize Then
            e.ThrowException = False
        End If
    End Sub



the error message is handled....But it is looping in the datagridview1_error event ...and still not displaying the value in the selected combobox...
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24073375
Once again please CHECK if the values is available in the datasource and if the COLUMN is the index 0 (1st column)
Here's an working example if you want to try:

Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        ' Creates a datatable
        Dim dt As New DataTable
        dt.Columns.Add("id", GetType(Integer)).AutoIncrement = True
        dt.Columns.Add("item", GetType(String))
 
        ' Adds some values
        For x As Int16 = 0 To 10
            Dim dr As DataRow = dt.NewRow
            dr("item") = "Item " & x.ToString
            dt.Rows.Add(dr)
        Next
 
        ' Create a new combo and adds it to the grid
        Dim combo As New DataGridViewComboBoxColumn
        combo.DataSource = dt.DefaultView
        combo.DisplayMember = "item"
        combo.ValueMember = "id"
 
        Me.DataGridView1.Columns.Add(combo)
 
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each row As DataGridViewRow In Me.DataGridView1.Rows
            row.Cells(0).Value = "Item 3"
        Next
    End Sub
 
    Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        If e.Context = DataGridViewDataErrorContexts.Formatting Or e.Context = DataGridViewDataErrorContexts.PreferredSize Then
            e.ThrowException = False
        End If
    End Sub
 
End Class

Open in new window

0
 

Author Closing Comment

by:yuvaratna
ID: 31566814
Thanks a lot ipaulino...I really hard tough time with this datagridview, Which very few of them are familiar with, U Helped me in many instances.....
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24075033
Glad I could help!
jpaulino
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

688 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