M
Malhavoc
I have a table in the Access DB which is structured this way:
ITEMID
FIELDNAME
FIELDVALUE
The Key is ITEMID,FIELDNAME
I need such a configuration because the items I need to store can have
whatever fields the user could like. A simple example of the data
contained could be:
ITEMID;FIELDNAME;FIELDVALUE
1;FIELDA;1111
1;FIELDB;2222
2;FIELDA;3333
2;FIELDB;4444
I need to load a datagrid in VB.NET displaying a table like this
(according to the above example):
ITEM;FIELDA;FIELDB
1;1111;2222
2;3333;4444
I used to just load the data from Access and then build the grid
directly from VB, but the records are becoming too many and this
approach is just too slow. I was thinking about creating the table I
need directly from a query, and then loading the result in a grid;
however, this is something I'm having problems doing. Could anyone help
me? Thanks.
Just a couple of notes:
- not every field is necessary present for each ITEM, so I need to
handle the possibility of DBNull entries for some fields.
- If it can help, I know which fieldnames I can get before querying the
DB
If, instead, you think I should focus on improving the VB code, here it
is:
'dtH is the table I need to fill, and it already has the correct table
schema.
Private Sub LoadCards(ByVal dtToLoad As DataTable)
Dim i As Integer
Dim rowcount As Integer
Dim row As DataRow
dtH.Clear()
rowcount = dtToLoad.Rows.Count - 1
i = 0
Do While i <= rowcount
row = dtH.NewRow
row.Item("ITEMID") = dtToLoad.Rows(i).Item("ITEMID")
Do Until dtToLoad.Rows(i).Item("ITEMID") <>
row.Item("ITEMID") OrElse i > rowcount
If dtToLoad.Rows(i).Item("FIELDNAME") IsNot
DBNull.Value Then
row.Item(dtToLoad.Rows(i).Item("FIELDNAME")) =
dtToLoad.Rows(i).Item("FIELDVALUE")
End If
i += 1
Loop
dtH.Rows.Add(row)
Loop
End Sub
ITEMID
FIELDNAME
FIELDVALUE
The Key is ITEMID,FIELDNAME
I need such a configuration because the items I need to store can have
whatever fields the user could like. A simple example of the data
contained could be:
ITEMID;FIELDNAME;FIELDVALUE
1;FIELDA;1111
1;FIELDB;2222
2;FIELDA;3333
2;FIELDB;4444
I need to load a datagrid in VB.NET displaying a table like this
(according to the above example):
ITEM;FIELDA;FIELDB
1;1111;2222
2;3333;4444
I used to just load the data from Access and then build the grid
directly from VB, but the records are becoming too many and this
approach is just too slow. I was thinking about creating the table I
need directly from a query, and then loading the result in a grid;
however, this is something I'm having problems doing. Could anyone help
me? Thanks.
Just a couple of notes:
- not every field is necessary present for each ITEM, so I need to
handle the possibility of DBNull entries for some fields.
- If it can help, I know which fieldnames I can get before querying the
DB
If, instead, you think I should focus on improving the VB code, here it
is:
'dtH is the table I need to fill, and it already has the correct table
schema.
Private Sub LoadCards(ByVal dtToLoad As DataTable)
Dim i As Integer
Dim rowcount As Integer
Dim row As DataRow
dtH.Clear()
rowcount = dtToLoad.Rows.Count - 1
i = 0
Do While i <= rowcount
row = dtH.NewRow
row.Item("ITEMID") = dtToLoad.Rows(i).Item("ITEMID")
Do Until dtToLoad.Rows(i).Item("ITEMID") <>
row.Item("ITEMID") OrElse i > rowcount
If dtToLoad.Rows(i).Item("FIELDNAME") IsNot
DBNull.Value Then
row.Item(dtToLoad.Rows(i).Item("FIELDNAME")) =
dtToLoad.Rows(i).Item("FIELDVALUE")
End If
i += 1
Loop
dtH.Rows.Add(row)
Loop
End Sub