Solved

Data Adapters / Datasets and Datagrid views

Posted on 2006-07-04
4
217 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
  • 2
4 Comments
 
LVL 8

Expert Comment

by:kaliyugkaarjun
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now