看板 Visual_Basic 關於我們 聯絡資訊
※ 引述《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
horngsh:你是要將rw這一列加入dt資料表嗎? 01/17 19:48
mindscold:是呀 01/18 08:22
horngsh:那應該要用dt.Rows.Add(rw) 01/18 10:13
mindscold:謝謝你,已經成功了 01/18 13:12
mindscold:加上去後不知道為什麼會變成一次加2行(1行有預設值 跟 1 01/18 13:16
mindscold:行空白行),如果保留e.newobject = rw 這一句的話,那麼 01/18 13:17
mindscold:只會增加有預設值的1行(正常) 01/18 13:17
horngsh:e.newobject = rw這一行要拿掉吧 01/18 17:12