473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to pull SQL data in to a DatagridView.......

Hello,
I am new to pulling data into VB and using DataGridViews..... so this
might be a dumb question but I can not seem to understand how to make this
work.

What I need acomplished:
Exec a select statement and out put the results to a DataGridView. The
columns must be created on the fly as the query may return different fields.

A small example I got to work to a List box. If you could convert this to a
DGV for me it would be great ....
Col1 - Full Name
Col2 - Address
oSQLcmd.Connection = oSQLconn
oSQLcmd.CommandType = CommandType.Text
oSQLcmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"

strSQLopen = OpenSQLConn() ' Open Connection

If oSQLconn.State = ConnectionState.Open Then
oSQLdr = oSQLcmd.ExecuteReader() 'executing the command and
assigning it to connection

While oSQLdr.Read()
ListBox1.Items.Add(oSQLdr.Item("FullName") & " " &
oSQLdr.Item("Address"))
End While
End If
oSQLdr.Close()

Thank you very much
Marcelo

Aug 7 '07 #1
6 2321
Hi Marcelo,

With the DataGridView a datatable is still working the best. You can use a
datareader however are always busy to synchronise the indexes from the
columns.

Just my opinion

Cor

"Marcelo" <Ma*****@discussions.microsoft.comschreef in bericht
news:5E**********************************@microsof t.com...
Hello,
I am new to pulling data into VB and using DataGridViews..... so this
might be a dumb question but I can not seem to understand how to make this
work.

What I need acomplished:
Exec a select statement and out put the results to a DataGridView. The
columns must be created on the fly as the query may return different
fields.

A small example I got to work to a List box. If you could convert this to
a
DGV for me it would be great ....
Col1 - Full Name
Col2 - Address
oSQLcmd.Connection = oSQLconn
oSQLcmd.CommandType = CommandType.Text
oSQLcmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"

strSQLopen = OpenSQLConn() ' Open Connection

If oSQLconn.State = ConnectionState.Open Then
oSQLdr = oSQLcmd.ExecuteReader() 'executing the command and
assigning it to connection

While oSQLdr.Read()
ListBox1.Items.Add(oSQLdr.Item("FullName") & " " &
oSQLdr.Item("Address"))
End While
End If
oSQLdr.Close()

Thank you very much
Marcelo
Aug 7 '07 #2
Cor,
Sory I did not completly understand you commant but could you show me the
code to take my list view and make it a Datagridview?

thanks
Marcelo

"Cor Ligthert[MVP]" wrote:
Hi Marcelo,

With the DataGridView a datatable is still working the best. You can use a
datareader however are always busy to synchronise the indexes from the
columns.

Just my opinion

Cor

"Marcelo" <Ma*****@discussions.microsoft.comschreef in bericht
news:5E**********************************@microsof t.com...
Hello,
I am new to pulling data into VB and using DataGridViews..... so this
might be a dumb question but I can not seem to understand how to make this
work.

What I need acomplished:
Exec a select statement and out put the results to a DataGridView. The
columns must be created on the fly as the query may return different
fields.

A small example I got to work to a List box. If you could convert this to
a
DGV for me it would be great ....
Col1 - Full Name
Col2 - Address
oSQLcmd.Connection = oSQLconn
oSQLcmd.CommandType = CommandType.Text
oSQLcmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"

strSQLopen = OpenSQLConn() ' Open Connection

If oSQLconn.State = ConnectionState.Open Then
oSQLdr = oSQLcmd.ExecuteReader() 'executing the command and
assigning it to connection

While oSQLdr.Read()
ListBox1.Items.Add(oSQLdr.Item("FullName") & " " &
oSQLdr.Item("Address"))
End While
End If
oSQLdr.Close()

Thank you very much
Marcelo

Aug 7 '07 #3
Just the essential part, I have sent another message, however it seems to be
somewhere in nowhere land and I have no copy from it. I am not using the
Hungarian notation as you do, that is not adviced in dotNet. There it is
Pascal notation with some exceptions.

Instead of your datareader

dim dt as datatable
cmd.fill(dt)
dgv.Datasource = dt

I assume that you don't want to concatenate the name and adress now
otherwise you can add a column.

http://msdn2.microsoft.com/en-us/library/632cdz7z.aspx

Cor

"Marcelo" <Ma*****@discussions.microsoft.comschreef in bericht
news:4C**********************************@microsof t.com...
Cor,
Sory I did not completly understand you commant but could you show me
the
code to take my list view and make it a Datagridview?

thanks
Marcelo

"Cor Ligthert[MVP]" wrote:
>Hi Marcelo,

With the DataGridView a datatable is still working the best. You can use
a
datareader however are always busy to synchronise the indexes from the
columns.

Just my opinion

Cor

"Marcelo" <Ma*****@discussions.microsoft.comschreef in bericht
news:5E**********************************@microso ft.com...
Hello,
I am new to pulling data into VB and using DataGridViews..... so
this
might be a dumb question but I can not seem to understand how to make
this
work.

What I need acomplished:
Exec a select statement and out put the results to a DataGridView.
The
columns must be created on the fly as the query may return different
fields.

A small example I got to work to a List box. If you could convert this
to
a
DGV for me it would be great ....
Col1 - Full Name
Col2 - Address
oSQLcmd.Connection = oSQLconn
oSQLcmd.CommandType = CommandType.Text
oSQLcmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"

strSQLopen = OpenSQLConn() ' Open Connection

If oSQLconn.State = ConnectionState.Open Then
oSQLdr = oSQLcmd.ExecuteReader() 'executing the command and
assigning it to connection

While oSQLdr.Read()
ListBox1.Items.Add(oSQLdr.Item("FullName") & " " &
oSQLdr.Item("Address"))
End While
End If
oSQLdr.Close()

Thank you very much
Marcelo

Aug 7 '07 #4
I have pasted this in your code, by the way, did you know that the Hungarian
method is rare used in dotNet.

I have typed this on the fly, so watch typos etc.

\\\
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"
conn.Open
dim dt as new datatable
Try
cmd.Fill(dt)
Catch ex as exception
MessageBox.Show (ex.Tostring)
Finally
conn.Close
End Try
dt.Columns.Add("Name",System.String,"FullName + " " + Address")
Listbox1.DataSource = dt
ListBox1.DataMember = "Name"
///

"Marcelo" <Ma*****@discussions.microsoft.comschreef in bericht
news:4C**********************************@microsof t.com...
Cor,
Sory I did not completly understand you commant but could you show me
the
code to take my list view and make it a Datagridview?

thanks
Marcelo

"Cor Ligthert[MVP]" wrote:
>Hi Marcelo,

With the DataGridView a datatable is still working the best. You can use
a
datareader however are always busy to synchronise the indexes from the
columns.

Just my opinion

Cor

"Marcelo" <Ma*****@discussions.microsoft.comschreef in bericht
news:5E**********************************@microso ft.com...
Hello,
I am new to pulling data into VB and using DataGridViews..... so
this
might be a dumb question but I can not seem to understand how to make
this
work.

What I need acomplished:
Exec a select statement and out put the results to a DataGridView.
The
columns must be created on the fly as the query may return different
fields.

A small example I got to work to a List box. If you could convert this
to
a
DGV for me it would be great ....
Col1 - Full Name
Col2 - Address
oSQLcmd.Connection = oSQLconn
oSQLcmd.CommandType = CommandType.Text
oSQLcmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"

strSQLopen = OpenSQLConn() ' Open Connection

If oSQLconn.State = ConnectionState.Open Then
oSQLdr = oSQLcmd.ExecuteReader() 'executing the command and
assigning it to connection

While oSQLdr.Read()
ListBox1.Items.Add(oSQLdr.Item("FullName") & " " &
oSQLdr.Item("Address"))
End While
End If
oSQLdr.Close()

Thank you very much
Marcelo

Aug 7 '07 #5
Cor,
I will look into the Pascal notation. Right now i have all the code in
Hungarian and I dont want to make mods till this works..... Below is the
code you have me with 3 errors I ws unable to repair. Also you dumped the
data into a listbox, I was looking for Datagridview... or shuold i wait till
i se thi code running.

Thanks again
Marcelo

oSQLcmd.Connection = oSQLconn
oSQLcmd.CommandType = CommandType.Text
oSQLcmd.CommandText = "SELECT nAME, password FROM t_dssemp"
oSQLconn.Open()
Dim dt As New DataTable
Try
oSQLcmd.Fill(dt) 'Error - 'Fill' is not a member of
system.data.sqlclient.sqlcommand
Catch ex As Exception
MessageBox.Show(ex.ToString)
Finally
oSQLconn.Close()
End Try
dt.Columns.Add("Name", System.String, "FullName") ' Error = 'Sting'
is not a type in 'System' and can not be used as a expression
Listbox1.DataSource = dt
ListBox1.DataMember = "Name" ' Error - DataMember is not a member of
system.windows.form.listbox
"Cor Ligthert[MVP]" wrote:
I have pasted this in your code, by the way, did you know that the Hungarian
method is rare used in dotNet.

I have typed this on the fly, so watch typos etc.

\\\
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"
conn.Open
dim dt as new datatable
Try
cmd.Fill(dt)
Catch ex as exception
MessageBox.Show (ex.Tostring)
Finally
conn.Close
End Try
dt.Columns.Add("Name",System.String,"FullName + " " + Address")
Listbox1.DataSource = dt
ListBox1.DataMember = "Name"
///

"Marcelo" <Ma*****@discussions.microsoft.comschreef in bericht
news:4C**********************************@microsof t.com...
Cor,
Sory I did not completly understand you commant but could you show me
the
code to take my list view and make it a Datagridview?

thanks
Marcelo

"Cor Ligthert[MVP]" wrote:
Hi Marcelo,

With the DataGridView a datatable is still working the best. You can use
a
datareader however are always busy to synchronise the indexes from the
columns.

Just my opinion

Cor

"Marcelo" <Ma*****@discussions.microsoft.comschreef in bericht
news:5E**********************************@microsof t.com...
Hello,
I am new to pulling data into VB and using DataGridViews..... so
this
might be a dumb question but I can not seem to understand how to make
this
work.

What I need acomplished:
Exec a select statement and out put the results to a DataGridView.
The
columns must be created on the fly as the query may return different
fields.

A small example I got to work to a List box. If you could convert this
to
a
DGV for me it would be great ....
Col1 - Full Name
Col2 - Address
oSQLcmd.Connection = oSQLconn
oSQLcmd.CommandType = CommandType.Text
oSQLcmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"

strSQLopen = OpenSQLConn() ' Open Connection

If oSQLconn.State = ConnectionState.Open Then
oSQLdr = oSQLcmd.ExecuteReader() 'executing the command and
assigning it to connection

While oSQLdr.Read()
ListBox1.Items.Add(oSQLdr.Item("FullName") & " " &
oSQLdr.Item("Address"))
End While
End If
oSQLdr.Close()

Thank you very much
Marcelo



Aug 11 '07 #6
Sorry Marcelo,

I did it to much by hand.

You need the dataadapter in it

dim da is new SQLDataAdapter(oSQLcom, oSQLconn)
da.Fill(dt)

binding a datagridview is so easy
dgrv.datasource = dt
(it had to be displaymember, I don't know why I wrote datamember)

I thought it was this
dt.datacolumn.add("Name", GetType(System.String), "FullName")

http://msdn2.microsoft.com/en-us/lib...datatable.aspx

Not tested again,

Cor

"Marcelo" <Ma*****@discussions.microsoft.comschreef in bericht
news:A4**********************************@microsof t.com...
Cor,
I will look into the Pascal notation. Right now i have all the code in
Hungarian and I dont want to make mods till this works..... Below is the
code you have me with 3 errors I ws unable to repair. Also you dumped the
data into a listbox, I was looking for Datagridview... or shuold i wait
till
i se thi code running.

Thanks again
Marcelo

oSQLcmd.Connection = oSQLconn
oSQLcmd.CommandType = CommandType.Text
oSQLcmd.CommandText = "SELECT nAME, password FROM t_dssemp"
oSQLconn.Open()
Dim dt As New DataTable
Try
oSQLcmd.Fill(dt) 'Error - 'Fill' is not a member of
system.data.sqlclient.sqlcommand
Catch ex As Exception
MessageBox.Show(ex.ToString)
Finally
oSQLconn.Close()
End Try
dt.Columns.Add("Name", System.String, "FullName") ' Error = 'Sting'
is not a type in 'System' and can not be used as a expression
Listbox1.DataSource = dt
ListBox1.DataMember = "Name" ' Error - DataMember is not a member
of
system.windows.form.listbox
"Cor Ligthert[MVP]" wrote:
>I have pasted this in your code, by the way, did you know that the
Hungarian
method is rare used in dotNet.

I have typed this on the fly, so watch typos etc.

\\\
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"
conn.Open
dim dt as new datatable
Try
cmd.Fill(dt)
Catch ex as exception
MessageBox.Show (ex.Tostring)
Finally
conn.Close
End Try
dt.Columns.Add("Name",System.String,"FullName + " " + Address")
Listbox1.DataSource = dt
ListBox1.DataMember = "Name"
///

"Marcelo" <Ma*****@discussions.microsoft.comschreef in bericht
news:4C**********************************@microso ft.com...
Cor,
Sory I did not completly understand you commant but could you show me
the
code to take my list view and make it a Datagridview?

thanks
Marcelo

"Cor Ligthert[MVP]" wrote:

Hi Marcelo,

With the DataGridView a datatable is still working the best. You can
use
a
datareader however are always busy to synchronise the indexes from the
columns.

Just my opinion

Cor

"Marcelo" <Ma*****@discussions.microsoft.comschreef in bericht
news:5E**********************************@microso ft.com...
Hello,
I am new to pulling data into VB and using DataGridViews..... so
this
might be a dumb question but I can not seem to understand how to
make
this
work.

What I need acomplished:
Exec a select statement and out put the results to a
DataGridView.
The
columns must be created on the fly as the query may return different
fields.

A small example I got to work to a List box. If you could convert
this
to
a
DGV for me it would be great ....
Col1 - Full Name
Col2 - Address
oSQLcmd.Connection = oSQLconn
oSQLcmd.CommandType = CommandType.Text
oSQLcmd.CommandText = "SELECT FullNAME, Address FROM
t_dssemp"

strSQLopen = OpenSQLConn() ' Open Connection

If oSQLconn.State = ConnectionState.Open Then
oSQLdr = oSQLcmd.ExecuteReader() 'executing the command
and
assigning it to connection

While oSQLdr.Read()
ListBox1.Items.Add(oSQLdr.Item("FullName") & " " &
oSQLdr.Item("Address"))
End While
End If
oSQLdr.Close()

Thank you very much
Marcelo

Aug 14 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rich | last post by:
Hello, I need to pull data from 2 tables that reside on the same sql server but 2 different databases. In Query Analyzer I can say this select t1.*, t2.* from tbl1 t1 join database2.dbo.tbl1...
2
by: Andy | last post by:
I have a C# application that is using a DataGridView (DGV). The DGV uses a table adapter to connect to the datasource, which uses a DataSet as the data. The DataSet has some constraints placed on...
2
by: Ivan | last post by:
I have a class Foo which have two property. I have a thread that do some process and update class Foo int B. I have a datagridview on main form. this datagridview data source to this class Foo and...
2
by: Rick Shaw | last post by:
Hi, I have a problem with the datagridview not refreshed when the application first appear on the screen. The datagridview display data from a table in the dataset. At the same time, I've added...
6
by: alexia.bee | last post by:
Hi all, I am new to C# so please bear me :). I have an app which uses datagridview. it has 8 columns. I also have struct which has 8 members(the same as the datagridview). when I add row to...
0
by: David Mayerovitch | last post by:
As an exercise in learning Visual Basic 2005, I am putting together a simple XML editor. I place on the form the objects DataGridView1 and DataSet1. ' Read an XML file into DataSet1:...
9
by: trint | last post by:
How can I retrieve all of the data from a datagridview? Any help is appreciated. Thanks, Trint
4
by: =?Utf-8?B?UmludSBHb3BhbGFrcmlzaG5hIFBpbGxhaQ==?= | last post by:
Hi All, I have an application that fetch data thru a store proc and display in a datagrid, but after successful execution of proc the data grid is not visible.Please go thru the following code...
2
by: hjin | last post by:
DataGridView takes very much time if I update a lot of data I have a DataGridView which has a DataSource (a DataTable) of 3000 rows. If I want to update one column of every rows in the DataTable,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.