databinding a datagridview

J

Jim in Arizona

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_connection=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
 
C

Cor Ligthert [MVP]

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
 
J

Jim in Arizona

Cor said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top