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.
> 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.