Re: [.NET] Unable to cast object of type

看板Visual_Basic作者 (喵)時間15年前 (2011/01/17 15:41), 編輯推噓3(305)
留言8則, 2人參與, 最新討論串3/3 (看更多)
※ 引述《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

01/17 19:48, , 1F
你是要將rw這一列加入dt資料表嗎?
01/17 19:48, 1F

01/18 08:22, , 2F
是呀
01/18 08:22, 2F

01/18 10:13, , 3F
那應該要用dt.Rows.Add(rw)
01/18 10:13, 3F

01/18 13:12, , 4F
謝謝你,已經成功了
01/18 13:12, 4F

01/18 13:16, , 5F
加上去後不知道為什麼會變成一次加2行(1行有預設值 跟 1
01/18 13:16, 5F

01/18 13:17, , 6F
行空白行),如果保留e.newobject = rw 這一句的話,那麼
01/18 13:17, 6F

01/18 13:17, , 7F
只會增加有預設值的1行(正常)
01/18 13:17, 7F

01/18 17:12, , 8F
e.newobject = rw這一行要拿掉吧
01/18 17:12, 8F
文章代碼(AID): #1DC_Aq2M (Visual_Basic)
文章代碼(AID): #1DC_Aq2M (Visual_Basic)