我有一个绑定到数据源的datagridview。我试图用datagridviewcomboboxcolumn
替换DGV的第一列,但它给了我以下例外:
无法将对象转换为类型&system; system.argumentexception'输入' system.windows.forms.iwin32window'
这是我的代码。
Public Sub formdata()
Try
Dim query As String = "select th.catalogue,th.name,pi.itemcode,th.itemcode,pi.party_code " _
& "from partyitem pi,itemhead th where th.itemcode=pi.itemcode and pi.party_code=" & valpartycode & " order by th.catalogue"
adap = New OleDbDataAdapter(query, conn)
ds = New DataSet
ds.Clear()
adap.Fill(ds, "partyitem")
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.Columns.Item(0).HeaderText = "CATALOGUE"
DataGridView1.Columns.Item(0).Width = 180
DataGridView1.Columns.Item(1).HeaderText = "NAME"
DataGridView1.Columns.Item(1).Width = 350
DataGridView1.Columns.Item(2).Visible = False
DataGridView1.Columns.Item(3).Visible = False
DataGridView1.Columns.Item(4).Visible = False
Dim query1 As String = "select catalogue from itemhead where itemcode not in" _
& "(select itemcode from partyitem where party_code=" & valpartycode & ") order by catalogue"
adapnew = New OleDbDataAdapter(query1, conn)
Dim ds1 As New DataSet
adapnew.Fill(ds1, "itemhead")
Me.DataGridView1.Columns.Remove("catalogue")
Dim datacol As New DataGridViewComboBoxColumn
With datacol
.Name = "catalogue"
.DataPropertyName = "catalogue"
.DataSource = ds1.Tables(0)
.ValueMember = "catalogue"
.DisplayMember = "catalogue"
.HeaderText = "CATALOGUE"
.FlatStyle = FlatStyle.Flat
.DropDownWidth = 160
.Width = 200
End With
Me.DataGridView1.Columns.Insert(0, datacol)
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
我在下面的datagridview中以另一种形式做了同样的事情,它工作得很好,而且代码就是这里。
Public Sub formdata()
Try
Dim query As String = "SELECT Itemcategory.categoryname," _
& "PartyDiscount.discount,PartyDiscount.cash_discount,PartyDiscount.turnover_discount" _
& " from Itemcategory,PartyDiscount where PartyDiscount.category=Itemcategory.categorycode" _
& " and PartyDiscount.party_code=" & valpartycode & " order by itemcategory.categoryname"
adap = New OleDbDataAdapter(query, conn)
ds = New DataSet
ds.Clear()
adap.Fill(ds, "partydiscount")
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.Columns.Item(0).HeaderText = "PRODUCT GROUP"
'DataGridView1.Columns.Item(0).Width = 170
DataGridView1.Columns.Item(1).HeaderText = "DISCOUNT"
DataGridView1.Columns.Item(1).Width = 90
DataGridView1.Columns.Item(2).HeaderText = "CASH DISCOUNT"
DataGridView1.Columns.Item(2).Width = 90
DataGridView1.Columns.Item(3).HeaderText = "TURNOVER DISCOUNT"
DataGridView1.Columns.Item(3).Width = 95
adapnew = New OleDbDataAdapter("Select categoryname from itemcategory where itemtype='F' order by categoryname", conn)
Dim dsnew As New DataSet
adapnew.Fill(dsnew, "itemcategory")
Me.DataGridView1.Columns.Remove("categoryname")
Dim datacol As New DataGridViewComboBoxColumn
With datacol
.Name = "categoryname"
.DataPropertyName = "categoryname"
.DataSource = dsnew.Tables(0)
.ValueMember = "categoryname"
.DisplayMember = "categoryname"
.HeaderText = "PRODUCT GROUP"
.FlatStyle = FlatStyle.Flat
.DropDownWidth = 160
.Width = 200
End With
Me.DataGridView1.Columns.Insert(0, datacol)
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
不知道为什么上面的代码出错了。请帮帮我。
修改
堆栈跟踪:
在MYsoft.PMitemlist.datagridview1_dataerror(Object sender,datagridviewdataerroreventargs e)中 E:\ Harry \ Mysoft \ MYSOFT \ PMitemlist.vb:list 226
编辑2:
Try
Dim a As Exception = e.Exception
Catch ex As Exception
MessageBox.Show(ex.StackTrace.ToString, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
它还提供以下异常
system.argumentexception:datagridviewcomboboxcell值无效