473,320 Members | 1,879 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.

databinding a datagridview

Sorry for the x-post. There seems to be very little if any activity on
the windowsforms group.

Nearly all my experience so far has been with web forms so I'm just
learning to work with windows forms.

I'm trying to bind data do a datagridview but dont' know how. This is
what I have so far.

========================================

Public Class frmSearch

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnSearch.Click
Dim strConnection, strStoredProc, strSQL As String
Dim strInput As String = "%" & txtSearch.Text & "%"
Dim NoMatch As Boolean = False
strConnection =
"server=sqlwebserver;database=directory;trusted_co nnection=true"
strStoredProc = "sp_getemployee"
Dim objConnection As New SqlConnection(strConnection)
Dim objCMD As New SqlCommand(strStoredProc, objConnection)
objCMD.CommandType = CommandType.StoredProcedure

Dim objEmpName As New SqlParameter("@EmpName", SqlDbType.VarChar)
objEmpName.Value = strInput
objEmpName.Direction = ParameterDirection.Input
objCMD.Parameters.Add(objEmpName)

Try
objConnection.Open()
frmResults.dgvResults.DataSource = objCMD.ExecuteReader()

frmResults.Show()
frmResults.Visible = True
frmResults.dgvResults.Visible = True

objConnection.Close()

Catch ex As Exception
frmErrorMessages.txtErrorMessage.Text = ex.Message.ToString
frmErrorMessages.Show()
End Try

End Sub
End Class

========================================
The application connects to the database ok. When I click the button,
the form with the datagridview shows up but the datagridview control is
empty. I do know that data is being returned since I run the same stored
procedure on a web form that works great. On a web form, however, I
would have to bind the data to the control after I set the Datasource
property. I don't know how that works with a web form though. Examples
that I find on the net are old and much more complicated than what it
probably should be (using data sets and sqlcommandbuilders and such).

TIA,
Jim
Mar 26 '07 #1
2 1414
Jim,

In ASPNet you can bind the datareader to the grids. It is in fact one
direction. In windowforms you have to bind a collection. In the case of
ADONET a datatable or a dataset.

Cor

"Jim in Arizona" <ti*******@hotmail.comschreef in bericht
news:ul**************@TK2MSFTNGP03.phx.gbl...
Sorry for the x-post. There seems to be very little if any activity on the
windowsforms group.

Nearly all my experience so far has been with web forms so I'm just
learning to work with windows forms.

I'm trying to bind data do a datagridview but dont' know how. This is what
I have so far.

========================================

Public Class frmSearch

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSearch.Click
Dim strConnection, strStoredProc, strSQL As String
Dim strInput As String = "%" & txtSearch.Text & "%"
Dim NoMatch As Boolean = False
strConnection =
"server=sqlwebserver;database=directory;trusted_co nnection=true"
strStoredProc = "sp_getemployee"
Dim objConnection As New SqlConnection(strConnection)
Dim objCMD As New SqlCommand(strStoredProc, objConnection)
objCMD.CommandType = CommandType.StoredProcedure

Dim objEmpName As New SqlParameter("@EmpName", SqlDbType.VarChar)
objEmpName.Value = strInput
objEmpName.Direction = ParameterDirection.Input
objCMD.Parameters.Add(objEmpName)

Try
objConnection.Open()
frmResults.dgvResults.DataSource = objCMD.ExecuteReader()

frmResults.Show()
frmResults.Visible = True
frmResults.dgvResults.Visible = True

objConnection.Close()

Catch ex As Exception
frmErrorMessages.txtErrorMessage.Text = ex.Message.ToString
frmErrorMessages.Show()
End Try

End Sub
End Class

========================================
The application connects to the database ok. When I click the button, the
form with the datagridview shows up but the datagridview control is empty.
I do know that data is being returned since I run the same stored
procedure on a web form that works great. On a web form, however, I would
have to bind the data to the control after I set the Datasource property.
I don't know how that works with a web form though. Examples that I find
on the net are old and much more complicated than what it probably should
be (using data sets and sqlcommandbuilders and such).

TIA,
Jim

Mar 27 '07 #2
Cor Ligthert [MVP] wrote:
Jim,

In ASPNet you can bind the datareader to the grids. It is in fact one
direction. In windowforms you have to bind a collection. In the case of
ADONET a datatable or a dataset.

Cor
Thanks again for your help, Cor. I'll go the datareader/table route then.
Mar 27 '07 #3

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

Similar topics

0
by: T.Haugnes | last post by:
I've installed visual studio c# express, .net SDK 2.0, SQL Server Express, created a table in the SQL database and filled this with some data-rows. I dragged the database-table from Data...
3
by: Bob | last post by:
I had a SQL server 2005 table in which I noticed I mispelled a column name. I changed the column name in the sql server table but after that, in the UI fortm using the table, the dataset control...
1
by: Dave A | last post by:
Hi, I am struggling with two way databinding in WinForms and the DataGridView. I am binding to business object classes (rather than datatables). If I have a collection of these business...
9
by: helpful sql | last post by:
Hi all, I have a DataGridView control in my Windows Forms application. I am binding it to a DataTable in another assembly. The name of the assembly is ActivityListDALC. I want to bind my grid to...
11
by: John J. Hughes II | last post by:
I have a DataGridView displaying data from a DataSet. To the right of that I have a custom user control which displays one of the data set fields. The custom user control is bound to the data set...
1
by: Pieter | last post by:
Hi, My problem: When the cursor is on a given row in the DataGridView, the data in the row has to be shown in TextBoxes underneath the DataGridView. the user must be able to edit in the...
3
by: jimmarq | last post by:
Please help me with this databinding scenario: Suppose I have a "Person" class, and that users populate Person objects in a BindingList collection through a datagridview. Users save the Person...
0
by: CSharpie1000 | last post by:
I am sorry for the broadness of this question, but I litterally can't get anywhere with this. I have a dataGridView that loads data from an Access 2007 Database. This works fine. I would like...
4
by: John Wright | last post by:
I have set up a datagrid on my form with six columns. I want to capture the data from six textboxes on the form and add them to the datagrid. All the info I can find about this is with datatables...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
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...
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
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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

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.