Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Binding to DataGrid

SLG29SLG29 Member Posts: 4
Can anyone help with this one

Have a form with a listbox and 5 textboxes, by clicking the different customer id's in the listbox this then displays the relevant info in the textboxes which is first name, last name, city, state and zip code, this works fine but I'm trying to display the order details for each selected order id in a datagrid but can't get it to work

The order details to be displayed in the datagrid from the database are OrderID, OrderPrice and CustomerID, do I need to incorporate a data relation here

This is the coding I have so far

Imports System.Data.OleDb
Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Declares the command object
Dim Sql As String = "SELECT * FROM customers"

'Declares the connection object
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\The Doughnut Shop.mdb"
'Declares and instantiates a new OleDbConnection object
Dim myConnection As OleDbConnection = New OleDbConnection(connString)
myConnection.ConnectionString = connString
'Declares and instantiates a new data adapter
Dim da As OleDbDataAdapter = New OleDbDataAdapter(Sql, myConnection)

'Declares and instantiates a new data set
Dim ds As DataSet = New DataSet
'Fills the data set
da.Fill(ds, "customers")

Dim recordNum As Integer

recordNum = ds.Tables("customers").Rows.Count - 1

Dim i As Integer
'Loops through the data set and adds the information accordingly
For i = 0 To recordNum
ListBox1.Items.Add(ds.Tables("customers").Rows(i).Item("Customerid"))
Next
TextBox1.Text = ds.Tables("customers").Rows(0).Item("FirstName")
TextBox2.Text = ds.Tables("customers").Rows(0).Item("LastName")
TextBox3.Text = ds.Tables("customers").Rows(0).Item("City")
TextBox4.Text = ds.Tables("customers").Rows(0).Item("State")
TextBox5.Text = ds.Tables("customers").Rows(0).Item("ZipCode")


End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim Sql As String = "SELECT * FROM customers"
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\The Doughnut Shop.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection(connString)
myConnection.ConnectionString = connString
Dim ds As New DataSet
Dim da As New OleDbDataAdapter(Sql, myConnection)

myConnection.ConnectionString = connString

da.Fill(ds, "customers")

Dim id As Integer
id = ListBox1.Text

TextBox1.Text = ds.Tables("customers").Rows(id - 1).Item("FirstName")
TextBox2.Text = ds.Tables("customers").Rows(id - 1).Item("LastName")
TextBox3.Text = ds.Tables("customers").Rows(id - 1).Item("City")
TextBox4.Text = ds.Tables("customers").Rows(id - 1).Item("State")
TextBox5.Text = ds.Tables("customers").Rows(id - 1).Item("ZipCode")

End Sub


End Class

Any coding/advice would be great

Thanks

Comments

  • Bharathi-kBharathi-k Member Posts: 22
    Hi,

    Try this article http://www.vkinotek.com/databinding.html.

    Regards
    bhar


    : Can anyone help with this one
    :
    : Have a form with a listbox and 5 textboxes, by clicking the different customer id's in the listbox this then displays the relevant info in the textboxes which is first name, last name, city, state and zip code, this works fine but I'm trying to display the order details for each selected order id in a datagrid but can't get it to work
    :
    : The order details to be displayed in the datagrid from the database are OrderID, OrderPrice and CustomerID, do I need to incorporate a data relation here
    :
    : This is the coding I have so far
    :
    : Imports System.Data.OleDb
    : Imports System.IO
    :
    : Public Class Form1
    : Inherits System.Windows.Forms.Form
    :
    :
    : Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    : 'Declares the command object
    : Dim Sql As String = "SELECT * FROM customers"
    :
    : 'Declares the connection object
    : Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\The Doughnut Shop.mdb"
    : 'Declares and instantiates a new OleDbConnection object
    : Dim myConnection As OleDbConnection = New OleDbConnection(connString)
    : myConnection.ConnectionString = connString
    : 'Declares and instantiates a new data adapter
    : Dim da As OleDbDataAdapter = New OleDbDataAdapter(Sql, myConnection)
    :
    : 'Declares and instantiates a new data set
    : Dim ds As DataSet = New DataSet
    : 'Fills the data set
    : da.Fill(ds, "customers")
    :
    : Dim recordNum As Integer
    :
    : recordNum = ds.Tables("customers").Rows.Count - 1
    :
    : Dim i As Integer
    : 'Loops through the data set and adds the information accordingly
    : For i = 0 To recordNum
    : ListBox1.Items.Add(ds.Tables("customers").Rows(i).Item("Customerid"))
    : Next
    : TextBox1.Text = ds.Tables("customers").Rows(0).Item("FirstName")
    : TextBox2.Text = ds.Tables("customers").Rows(0).Item("LastName")
    : TextBox3.Text = ds.Tables("customers").Rows(0).Item("City")
    : TextBox4.Text = ds.Tables("customers").Rows(0).Item("State")
    : TextBox5.Text = ds.Tables("customers").Rows(0).Item("ZipCode")
    :
    :
    : End Sub
    :
    : Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    : Dim Sql As String = "SELECT * FROM customers"
    : Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\The Doughnut Shop.mdb"
    : Dim myConnection As OleDbConnection = New OleDbConnection(connString)
    : myConnection.ConnectionString = connString
    : Dim ds As New DataSet
    : Dim da As New OleDbDataAdapter(Sql, myConnection)
    :
    : myConnection.ConnectionString = connString
    :
    : da.Fill(ds, "customers")
    :
    : Dim id As Integer
    : id = ListBox1.Text
    :
    : TextBox1.Text = ds.Tables("customers").Rows(id - 1).Item("FirstName")
    : TextBox2.Text = ds.Tables("customers").Rows(id - 1).Item("LastName")
    : TextBox3.Text = ds.Tables("customers").Rows(id - 1).Item("City")
    : TextBox4.Text = ds.Tables("customers").Rows(id - 1).Item("State")
    : TextBox5.Text = ds.Tables("customers").Rows(id - 1).Item("ZipCode")
    :
    : End Sub
    :
    :
    : End Class
    :
    : Any coding/advice would be great
    :
    : Thanks
    :
    :

Sign In or Register to comment.