Solved

Data Adapters / Datasets and Datagrid views

Posted on 2006-07-04
4
220 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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