Link to home
Start Free TrialLog in
Avatar of K Feening
K FeeningFlag for Australia

asked on

Vb.net Listview

HI Experts

I have an SQL statement
SELECT Name, Address, Suburb, State FROM ADDRESSES 

Open in new window

Is there a way to add the SQL Select Field names to a Listview
e.g
me.listview.Columns.Add(SQL.Field(0), 100, HorozontalAlignment.left)
me.listview.Columns.Add(SQL.Field(1), 100, HorozontalAlignment.left)

Thanks
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

what's the Object type for your object SQL ?

quick guess, you probably can try: SQL.Field(0).Name ?
Avatar of K Feening

ASKER

In SQL server you run a script and it adds the Column headers in VB when the user can change the SQL from
 
SELECT Name, Address, Suburb, State FROM ADDRESSES 

Open in new window


To

 
SELECT Number, Name, Address, Suburb, State, Postcode FROM ADDRESSES 

Open in new window


How do you get the Column Header Number, Name etc and number of columns 6
I think I'm not clear enough in my question above.

what I mean is:

>>SQL.Field(0)

what is this SQL object?

To answer your other questions we need to understand what is this object.
Hi Kevinfeening,

To bind listview with sql result, you'll have to take the table returned from sql in a datatable and bind your listview with that datatable.

Try below code by passing your connection string, your sql query and your listview.
        Dim constr As String = "Put your connection string here"
        Dim ds As New DataSet
        Dim con As New SqlClient.SqlConnection(constr)
        con.Open()
        Dim sqladap As New SqlClient.SqlDataAdapter("SELECT Number, Name, Address, Suburb, State, Postcode FROM ADDRESSES ", con)
        sqladap.Fill(ds)
 
        For i As Integer = 0 To ds.Tables(0).Columns.Count – 1
            ListView1.Columns.Add(ds.Tables(0).Columns(i).ToString())
        Next
        For i As Integer = 0 To ds.Tables(0).Rows.Count – 1
            Dim listRow As New ListViewItem
            listRow.Text = ds.Tables(0).Rows(i)(0).ToString()
            For j As Integer = 1 To ds.Tables(0).Columns.Count – 1
                listRow.SubItems.Add(ds.Tables(0).Rows(i)(j).ToString())
            Next
            ListView1.Items.Add(listRow)
        Next

Open in new window

No it was a example of what I thought could be done no such thing as SQL.Field(0)
its a plain SQL SELECT statement which runs in 2008 Server and I need to get the heading when running in VB.net
using Dim Reader as Data.IDaterReader = DataConnector.GetReader(SQL,Nothing)

Do While reader.read = true 
loop

Open in new window

Here columns in datatable will be same as what you are getting from your sql query. You can use column name of datatable in your list view.
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Simple and worked
Others more complicated but worked

Thanks All