作者mindscold (喵)
看板Visual_Basic
标题Re: [.NET] Unable to cast object of type
时间Mon Jan 17 15:41:37 2011
※ 引述《horngsh ()》之铭言:
: ※ 引述《mindscold (喵)》之铭言:
: : Dim Table As DataTable = DirectCast(Me.myBindingSource.DataSource, DataTable)
: : 并且出现以下的错误讯息:
: : "Unable to cast object of type 'PICData.PICSettingDataSet' to type
: : 'System.Data.DataTable'
: : 请问为什麽datasource也是table却不能转换成datatable?
: : 该怎麽改才是对的呢?谢谢
: Try
: Dim ds as DataSet = CType(Me.myBindingSource.DataSource, DataSet)
: Catch ex As Exception
: MsgBox(ex.Message)
: End Try
: Dim dt1 as DataTable = ds.Tables(0)
抱歉,再请教一下
关於这部份我已经成功了
现在是
Private Sub PICsMBindingSource_AddingNew(ByVal sender As System.Object, ByVal
e As System.ComponentModel.AddingNewEventArgs) Handles
PICsMBindingSource.AddingNew
Dim Ds As DataSet
Try
Ds = CType(Me.PICsMBindingSource.DataSource, DataSet)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Dim dt As DataTable = Ds.Tables(PICsM)
Dim rw As DataRow = dt.NewRow
rw("MPartID") = getMPartID()
e.newobject = rw
^^^^^^^^^^^^^^^^
跑这行的时候会有错误:
'Objects added to a BindingSource's list must all be of the same type.'
这也是因为来源是dataset而非datatable吗?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 60.248.17.41
1F:推 horngsh:你是要将rw这一列加入dt资料表吗? 01/17 19:48
2F:→ mindscold:是呀 01/18 08:22
3F:推 horngsh:那应该要用dt.Rows.Add(rw) 01/18 10:13
4F:→ mindscold:谢谢你,已经成功了 01/18 13:12
5F:→ mindscold:加上去後不知道为什麽会变成一次加2行(1行有预设值 跟 1 01/18 13:16
6F:→ mindscold:行空白行),如果保留e.newobject = rw 这一句的话,那麽 01/18 13:17
7F:→ mindscold:只会增加有预设值的1行(正常) 01/18 13:17
8F:推 horngsh:e.newobject = rw这一行要拿掉吧 01/18 17:12