Solved

Data Adapters / Datasets and Datagrid views

Posted on 2006-07-04
4
222 Views
Last Modified: 2012-06-21
Hi I am new to this area of programming - so please go easy with me :-) can someone in simple terms please give me an example of how to programme at runtime and then link together, a datadapter, a dataset and then show me how to display the results in a datagrid.

To make it easier for me to follow can the example use the following criteria : a SQL database called "LiveDBF" with one table called "Stores". The stores table has three string fields (strName, strNumber, StrPhone).

Many thanks
...Steve
0
Comment
Question by:swtiley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17040696
Hi

Please check out the following Link

http://www.vb-helper.com/howto_net_datagrid.html

Or

Private Const SELECT_STRING As String = _
    "SELECT * FROM Stores"

Private Const CONNECT_STRING As String = _
    "Data Source=Bender\NETSDK;Initial " & _
        "Catalog=Contacts;User Id=sa"
Change this line with ur connection string

' The DataSet that holds the data.
Private m_DataSet As DataSet

' Load the data.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As _
    System.EventArgs) Handles MyBase.Load
    Dim data_adapter As SqlDataAdapter

    ' Create the SqlDataAdapter.
    data_adapter = New SqlDataAdapter(SELECT_STRING, _
        CONNECT_STRING)

    ' Map Table to Contacts.
    data_adapter.TableMappings.Add("Table", "Stores")

    ' Fill the DataSet.
    m_DataSet = New DataSet()
    data_adapter.Fill(m_DataSet)

    ' Bind the DataGrid control to the Stores DataTable.
    dgStores.SetDataBinding(m_DataSet, "Stores")
End Sub
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17040755
Hi

Also check out the following Link

http://www.codeproject.com/aspnet/datagriddemo.asp

Hope this  will help u  : )

Cheers!!!

0
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 17040849
Assuming that you have dragged a datagrid onto a form, all you need is seven steps.  Those can be accomplished in seven lines of code or, if you combine some of the steps, even less.  These are the steps.
 
1)  Make a connection string
2)  Make a connection, using (1)
3)  Make an SQL Select string
4)  Make a dataadapter using with (2) and (3)
5)  Make a blank, empty datatable
6)  Structure and fill (5) using (4)
7)  Set (5) as the .DataSource of your datagrid.

This is example code.  You would need Imports System.Data.SqlClient
 at the start of your form
 
   'Step 1: you can get variations from http://www.connectionstrings.com/
   'for the example you give, '<yourDataBase>' would be replaced by 'LiveDDF' - no quotes
   Dim strCon As String = "Data Source=<yourDataSource>;Initial Catalog=<yourDataBase>;Integrated Security=SSPI;"
   'Step 2
   Dim Con As New SqlConnection(strCon)
   'Step 3
   'for the example you give, '<yourTable>' would be replaced by 'Stores' - no quotes
   Dim strSQL As String = "SELECT * FROM <yourTable>"
   'Step 4
   Dim da As New SQLDataAdapter(strSQL, Con)
   'Step 5
   Dim dt As New DataTable
   'Step 6
   da.Fill(dt)
   'Step 7
   myDataGrid.DataSource = dt

That's it.  No bells or whistles: everything would be "default".  You might want to alter some of those.  You might want to do some of the declarations at the form start, to give the objects scope for all procedures within the form.

I see that, while typing the above, kaliyugkaarjun has given you an answer.  I agree with everything s/he says but still decided to post this as it is slightly simpler.  E.g., to make things work you don't actually need either a dataset or the table mappings, although there may be good reasons why - in a particular context - that more detailed approach should should be preferred.

Roger
0
 
LVL 1

Author Comment

by:swtiley
ID: 17043898
Thank you Roger,

This is exactly what I needed to get me going again, Thanks for the very clear step by step instructions.

Cheers
...Steve
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question