0

I am new here and in the world of programming. What I need to do is fill a datagridview from a query that I make through a method called "DoQuery", which is inside the "SAPbobsCOM" package. I would really appreciate if someone could help me with this. I know that using sql is as simple as putting a "Fill", but they don't work the same way because I don't use a datatable or dataadapter.

Sub llenarGrillaPrueba()
    Dim Count As Long
    Dim i As Long
    Dim RecSet As SAPbobsCOM.Recordset
    RecSet = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

    'RecSet.DoQuery("select ""ItemCode"" from OITM where ""ItemCode""='429'") 'If I only retrieve one record, the grid is loaded.

    RecSet.DoQuery("select * from OITM where ""InvntItem"" = 'Y' ORDER BY ""ItemCode"" ASC") 'The problem arises when I want to show all the records.
    Count = RecSet.RecordCount
    MsgBox(Count)

    While RecSet.EoF = False

        For i = 0 To Count - 1

            DataGridView1.Rows(i).Cells("codigo").Value = RecSet.Fields.Item("ItemCode").Value
            RecSet.MoveNext()
        Next i

    End While

End Sub

Please if anyone can help me I will be very grateful.

Andrew Mortimer
  • 2,380
  • 7
  • 31
  • 33
  • I'm not sure whether you can bind a `Recordset` in .NET or not but that's the first thing I'd try. Add a `BindingSource` to your form and then, in code, assign the `Recordset` to its `DataSource` property. If that works, you can just bind the `BindingSource` to the grid and you're done. – user18387401 Apr 09 '22 at 02:16
  • If binding doesn't work, which seems likely, then you need to modify the code you have to actually add rows to the grid. Think about what the code you have actually does. It goes to the first row in the `Recordset`, then it tries to set the cell values of a row in the grid for every row in the `Recordset`. Does that make any sense, particularly given the grid contains no rows? Think about the logic first and then write code to implement that logic. For each row in the `Recordset`, add one row to the grid and then transfer the field values from the `Recordset` to that row. – user18387401 Apr 09 '22 at 02:20
  • Thanks for your time. I was trying many things to make it work. I'm actually new to this world and I don't really know what I'm doing. I don't know how the recordset works, but I think I've found a way to make it work and in fact some of the code already works, but I don't know if it's the proper way to do it. The important thing is that it works for now before the tests carried out. – Alejandro Vecca Apr 09 '22 at 13:30

0 Answers0