在VB.NET中未处理InvalidCastException [英] InvalidCastException was unhandled in VB.NET

查看:207
本文介绍了在VB.NET中未处理InvalidCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的程序员,



我的目标是将行没有数据源的datagridview移动到另一个数据绑定形式的datagridview。当我单击按钮时,行移动/保存到数据限制的数据网格视图。



所以这是我的以下代码:

Dear fellow programmers,

My goal is to move rows from a datagridview that has no datasource to another datagridview in another form that is data-bounded. When I click the button the rows move/save to that data-bounded datagridview.

So here is my following code:

' Copy rows from the first datagridview to the second datagridview that is data bound
    ' First copy the second datagridviews datasource into a new data table

    Dim dt As DataTable = CType(frmEncodeDatabase.EncodingDataGridView.DataSource, DataTable).Copy
    Dim dr As DataRow

    ' Loop through all rows of the first datagridview and copy them into the data table

    For r As Int32 = 0 To Me.DataGridViewX1.Rows.Count - 1
        If Me.DataGridViewX1.Rows(r).IsNewRow = False Then   ' Do not copy the new row
            dr = dt.NewRow

            ' Loop through all cells of the first datagridview and populate the data row

            For c As Int32 = 0 To Me.DataGridViewX1.ColumnCount - 1
                dr(c) = Me.DataGridViewX1.Rows(r).Cells(c).Value
            Next

            dt.Rows.Add(dr) ' Add the data row to the data table
        End If
    Next

    Me.DataGridView2.DataSource = dt    ' Rebind the second datagridview with the new table which has all the rows from both datagridviews
    frmEncodeDatabase.show()





因此,



So there comes the error at

dt As DataTable = CType(frmEncodeDatabase.EncodingDataGridView.DataSource, DataTable).Copy

。它说 InvalidCastException未处理



为了更好地查看错误,请查看链接(图片):http://postimg.org/image/k65b52bxn/ [ ^ ]





任何帮助/建议/代码将不胜感激,

Genesis

. It says InvalidCastException was unhandled.

To get a better view on the error, please look at the link (image): http://postimg.org/image/k65b52bxn/[^]


Any help/suggestion/code will be appreciated,
Genesis

推荐答案

好吧,首先使用teh调试器来确切了解 frmEncodeDatabase.EncodingDataGridView.DataSource 究竟是什么。

直到你知道,你只是猜测...



所以在线上放一个断点,运行你的应用程序,当它点击时看看价值观。



但是如果没有设置值的代码(无论如何),我们无法再帮助你了。
Well, start by using teh debugger to find out exactly what frmEncodeDatabase.EncodingDataGridView.DataSource actually is.
Until you know that, you are just guessing...

So put a breakpoint on the line, run your app and when it hits it, look at the values.

But without the code that sets the value (where ever that is) we can't help you any further.


这篇关于在VB.NET中未处理InvalidCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆