ListView
-
Dim dtrReader As SqlDataReader = cmdCommand.ExecuteReader() Try While dtrReader.Read() lstViewBuyers.Items.Add(dtrReader(1)) End While Finally dtrReader.Close() End Try this is how i display one field from the database to the listview... but my problem is when i come to display one row/record from the database to the listview... How can i display the whole record in the sql database to using listview control? pls help
nothing is impossible.....
-
Dim dtrReader As SqlDataReader = cmdCommand.ExecuteReader() Try While dtrReader.Read() lstViewBuyers.Items.Add(dtrReader(1)) End While Finally dtrReader.Close() End Try this is how i display one field from the database to the listview... but my problem is when i come to display one row/record from the database to the listview... How can i display the whole record in the sql database to using listview control? pls help
nothing is impossible.....
By using a string builder to build a string out of the data in the row, and then adding that. (1) is returning the second column, you can access them by name or index ( name is more self documenting ), you can build the string any way you like. There's no way to make the string build itself.
Christian Graus - C++ MVP
-
Dim dtrReader As SqlDataReader = cmdCommand.ExecuteReader() Try While dtrReader.Read() lstViewBuyers.Items.Add(dtrReader(1)) End While Finally dtrReader.Close() End Try this is how i display one field from the database to the listview... but my problem is when i come to display one row/record from the database to the listview... How can i display the whole record in the sql database to using listview control? pls help
nothing is impossible.....
one way but not a good way is concatinate all of your field together that the query result is become to 1 field e.g. sql --> "select f1 + ' ' + f2 + ' ' + f3 + ' ' + f4 from TableName" ' ' is white space, f1 to f4 is field name result --> "aaa aaa aaa aaa" "bbb bbb bbb bbb" "cccc ccc cc ccccc" the output format is not order in column but it's ok if the data is name and surname :-D
-
Dim dtrReader As SqlDataReader = cmdCommand.ExecuteReader() Try While dtrReader.Read() lstViewBuyers.Items.Add(dtrReader(1)) End While Finally dtrReader.Close() End Try this is how i display one field from the database to the listview... but my problem is when i come to display one row/record from the database to the listview... How can i display the whole record in the sql database to using listview control? pls help
nothing is impossible.....
Hi, I have solved this problem simply as follows:
lstViewBuyers.Items.Add(dtrReader(1) & "|" & dtrReader(2))
I.e. you just concatenate all the fields from the record into a string with a divider between the values. Afterward, if you want to use any part of the record, selected from the listview, you just itirate through the string. The | character (ofcourse you can use any other character or string) serves as the divider. Something like this:Dim divider As String = "|" Dim cut As Integer Dim TheBitYouNeed As String cut = TheBitYouNeed.IndexOf(divider) TheBitYouNeed= Mid(TheBitYouNeed, 1, cut)
Hope this helps you, Johan -
Dim dtrReader As SqlDataReader = cmdCommand.ExecuteReader() Try While dtrReader.Read() lstViewBuyers.Items.Add(dtrReader(1)) End While Finally dtrReader.Close() End Try this is how i display one field from the database to the listview... but my problem is when i come to display one row/record from the database to the listview... How can i display the whole record in the sql database to using listview control? pls help
nothing is impossible.....
Hey, another way of doing this if you don't know the amount of columns in the table you are getting would be:
Try While dtrReader.Read() Dim lvItem As New ListViewItem(dtrReader(0)) For i As Integer = 1 To dtrReader.FieldCount - 1 lvItem.SubItems.Add(dtrReader(i)) Next lstViewBuyers.Items.Add(lvItem) End While Finally dtrReader.Close() End Try
Bsically the same thing, but since the data reader has field information on it, we just use that and create the sub items from the datareader starting at the 1 position. Have you thought of using a bindable list view? You may want to check to see if nulls are coming back though, you may get exceptions when you get data from a datareader that has null values and try to add it to a list view item like this.