3 Replies - 571 Views - Last Post: 02 December 2013 - 09:38 AM Rate Topic: -----

#1 nyt1972  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 04-February 10

Unable to cast object of type 'System.Windows.Forms.DataGridViewRo

Posted 02 December 2013 - 01:01 AM

Hello,

I want to query a datagridview, below is my code but it shows error:

Unable to cast object of type 'System.Windows.Forms.DataGridViewRow[]' to type 'System.Windows.Forms.DataGridViewRow'.

in this line:
TblFeePaymentDataGridView.Rows.AddRange(rows)

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim rows = (From r As DataGridViewRow In TblFeePaymentDataGridView.Rows Where CType(r, DataGridViewRow).Cells("forMonth").Value <> "").Cast(Of DataGridViewRow)().ToArray
        '  Dim rows = "(From row As DataGridViewRow In TblFeePaymentDataGridView.Rows.Cast(Of DataGridViewRow)() Where Not row.IsNewRow AndAlso (row.Cells('forMonth').Value.ToString =" & ComboBox1.SelectedText & " Select row).ToArray"
        ' TblFeePaymentDataGridView.Rows.Clear()
        TblFeePaymentDataGridView.Rows.AddRange(rows)
    End Sub


Please help.

Regards.

This post has been edited by nyt1972: 02 December 2013 - 01:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Unable to cast object of type 'System.Windows.Forms.DataGridViewRo

#2 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 612
  • View blog
  • Posts: 2,741
  • Joined: 03-December 12

Re: Unable to cast object of type 'System.Windows.Forms.DataGridViewRo

Posted 02 December 2013 - 07:00 AM

try,

TblFeePaymentDataGridView.Rows.AddRange(rows())

Was This Post Helpful? 0
  • +
  • -

#3 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 236
  • View blog
  • Posts: 781
  • Joined: 01-February 13

Re: Unable to cast object of type 'System.Windows.Forms.DataGridViewRo

Posted 02 December 2013 - 07:24 AM

Hi,
It looks like you are trying to get a list of the rows that have a value other than Nothing or an Empty String in the "forMonth" column and then clearing all the rows in the datagridview and then adding all the rows back into the datagridview. If this is the case then i would just loop threw all the rows of the datagridview and if the "forMonth" column is equal to "Nothing" or an Empty String then just remove or hide that row. Removing it means it is gone untill you load the data again and if you set the rows Visible Property to false to hide it then it will still be there but, just not visible. Anyways, maybe you can do something like this.
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TblFeePaymentDataGridView.Rows.Add("John Doe", "22.52")
        TblFeePaymentDataGridView.Rows.Add("Steve S.") 'No value for "forMonth" is set so it is equal to Nothing
        TblFeePaymentDataGridView.Rows.Add("Larry Newman", "5.94")
        TblFeePaymentDataGridView.Rows.Add("Kim Pratt", "") '"forMonth" is equal to an empty string
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        For Each r As DataGridViewRow In TblFeePaymentDataGridView.Rows
            If Not r.IsNewRow AndAlso (r.Cells("forMonth").Value Is Nothing OrElse r.Cells("forMonth").Value.ToString = "") Then
                TblFeePaymentDataGridView.Rows.Remove(r) 'Use this if you actually want to remove the rows

                'r.Visible = False 'Use this if you just want to hide the rows but, not actually remove them
            End If
        Next
    End Sub

End Class


Was This Post Helpful? 0
  • +
  • -

#4 nyt1972  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 04-February 10

Re: Unable to cast object of type 'System.Windows.Forms.DataGridViewRo

Posted 02 December 2013 - 09:38 AM

I tried this
TblFeePaymentDataGridView.Rows.AddRange(rows())


and it gives the error:
Attempted to operate on an array with the incorrect number of dimensions.

then trien IronRazer's method but it does nothing. then tried the below one but it also does nothing

 For Each r As DataGridViewRow In TblFeePaymentDataGridView.Rows
            For Each c As DataGridViewCell In r.Cells
                If Not r.IsNewRow AndAlso (r.Cells("forMonth").Value Is Nothing OrElse r.Cells("forMonth").Value.ToString = "") Then
                    TblFeePaymentDataGridView.Rows.Remove(r) 'Use this if you actually want to remove the rows
                    'r.Visible = False 'Use this if you just want to hide the rows but, not actually remove them
                End If
            Next
        Next

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1