Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2007

Tip: Looking for answers? Try searching our database.

Databinding a datagridview control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jim in Arizona - 26 Mar 2007 18:38 GMT
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
Eric - 28 Mar 2007 02:47 GMT
If you want an easy solution.

1- Create a dataset in your project.
2- Follow the wizard to create a datatable/tableadapter with the query to
populate your grid.
3- Save it.
4- Click the tag icon on the top right corner of your datagridview.
5- In the Choose data source combobox, select Other Sources, Projet Data
Source and the Datatable you just created.
6- This will create a Dataset instance, a Bindingsource object and a
tableadapter object in your form.
7- In the form load event, fill your grid using the tableadapter. Use the
following line of code (adapt it):
Me.YourTableAdapter.Fill(Me.YourDataset)

There a few ways to populate a datagrid, this is the easiest.

> Nearly all my experience so far has been with web forms so I'm just
> learning to work with windows forms.
[quoted text clipped - 53 lines]
> TIA,
> Jim
Jim in Arizona - 29 Mar 2007 18:36 GMT
> If you want an easy solution.
>
[quoted text clipped - 12 lines]
>
> There a few ways to populate a datagrid, this is the easiest.

Thanks for the info, Eric. I'll give that a try, hopefully, before the
week is out (I got stuck on a few other projects that need to get done
first) and I'll let you know how it turns out for me.

I typically try to hard code everything without the use of wizards but I
see that, eventually, I'll just have to give in and do things the 'new
and improved' way(s).
RobinS - 30 Mar 2007 01:29 GMT
>> If you want an easy solution.
>>
[quoted text clipped - 20 lines]
> see that, eventually, I'll just have to give in and do things the 'new
> and improved' way(s).

You can code it yourself if you want to. I'd rather do that than use a
wizard (I have control issues).

BTW, in WinForms, you can't data bind to a DataReader.

Dim ds as DataSet
Using cn as New SqlConnection(myConnectionString)
 cn.Open()
 Dim cmd as New SqlCommand()
 cmd.Connection = cn
 cmd.CommandType = CommandType.StoredProcedure
 cmd.CommandText = "sp_getemployee"
 cmd.Parameters.AddWithValue("@EmpName", strInput)
 Dim da as SqlDataAdapter = New SqlDataAdapter()
 ds = New DataSet()
 da.Fill(ds, "Employees")
End Using
Dim myBindingSource As BindingSource = New BindingSource
myBindingSource.DataSource = myDataSet.Tables("Employees")
myDataGridView.DataSource = myBindingSource;

You can also use a DataTable instead of a DataSet, in which case it becomes
 da.Fill(dt)
and
 myBindingSource.DataSource = dt

Brian Noyes' data binding book has a *lot* of code in it, and it's been
really helpful to me. His and Dave Sceppa's ADO.Net Core Reference.  Very
useful.

Good luck.
Robin S.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.