Results 1 to 6 of 6

Thread: How to load datagrid into dataset in vb.net

  1. #1
    Join Date
    Dec 2010
    Posts
    68

    How to load datagrid into dataset in vb.net

    I am using visual studo 2010 academic and ms access 2007. I want to load datagrid into dataset, I have drgged text boxes and datagrid to the form. I can insert text box inputs into the datagrid. How do i load datagrid into dataset in order to export datagrid in access table. Please give me the step by step procedure. Please help me out, your small help will be appreciated.

  2. #2
    Join Date
    Nov 2008
    Posts
    1,221

    Re: How to load datagrid into dataset in vb.net

    For loading datagrid into dataset you should write the following code and use in your program:
    Private Sub cboUserName_SelectedIndexChanged(ByVal sender As
    System.Object, _
    ByVal e As System.EventArgs) Handles
    cboUserName.SelectedIndexChanged

    Dim lngUserID As Long

    For i As Integer =3D 0 To ALWDBDataSet.Tables
    ("tblUser").Rows.Count - 1
    If ALWDBDataSet.Tables("tblUser").Rows(i).Item("UserName")
    =3D cboUserName.Text.ToUpper Then
    lngUserID =3D ALWDBDataSet.Tables("tblUser").Rows(i).Item
    ("UserID")
    Exit For
    End If
    Next

    '// Initialize a new instance of the OleDbConnection Class
    objConnection =3D New OleDbConnection(strConncectionString)

    '// Build query string
    Dim strSQL As String =3D "SELECT * FROM tblUserLogs WHERE UserID
    =3D " & lngUserID
    '// Initialize a new instance of the OleDbCommand Class
    objCommand =3D New OleDbCommand(strSQL, objConnection)

    Call PopulateGrid(dgvLogInfo, "tblUserLogs")


    End Sub

  3. #3
    Join Date
    Nov 2008
    Posts
    1,022

    Re: How to load datagrid into dataset in vb.net

    I think you have missed some code in your program for loading datagrid into dataset you should follow the code which is given below:
    Initially:

    dataAdapter1.SelectCommand.CommandText = "Select fld1, fld2, fld3 from
    tbl1"
    dataAdapter1.Fill(dataset1, "tbla")
    datagridview1.datasource = dataset1.Tables("tbla")

    Now a new query using a different table and a different number of
    fields:

    dataset1.Tables.Remove(dataset1.Tables("tbla"))
    datagridview1.Columns.Clear

    dataAdapter1.SelectCommand.CommandText = "Select fld5, fld6, fld7, fld8,
    fld9 from tbl2"
    dataAdapter1.Fill(dataset1, "tbla")
    datagridview1.datasource = dataset1.Tables("tbla")

    It seems like VS remembers the original table it creates and the
    original number of columns. So you have to remove the table from the
    dataset and remove the columns from the datagridview each time. Then
    run the new query.

    If your problem not solved try to search on internet related to this topic you’ll surely get the solution.

  4. #4
    Join Date
    May 2008
    Posts
    860

    Re: How to load datagrid into dataset in vb.net

    You have not mention any error while doing this, have you try to load datagrid into dataset, if yes then you should mention the problem or error while doing that, if no than try this code:
    Private Sub PopulateGrid(ByVal grd As DataGridView, ByVal strTable
    As String)

    '// Initialize a new instance of the OleDataAdapter class
    objDataAdapter =3D New OleDbDataAdapter

    '// Initialize a new instance of the DataSet Class
    objDataSet =3D New DataSet

    '// Set the SelectCommand for the OleDbDataAdapter
    objDataAdapter.SelectCommand =3D objCommand

    Try
    '// Poplate the DataSet
    objDataAdapter.Fill(objDataSet, strTable)

    '// Bind the DataSet to the DataGrid
    grd.DataSource =3D objDataSet
    grd.DataMember =3D strTable

    '// Set the AlternatingRowsDefaultCellStyle.BackColor
    property
    grd.AlternatingRowsDefaultCellStyle.BackColor =3D
    Color.WhiteSmoke

    '// Set the CellBorderStyle property
    grd.CellBorderStyle =3D DataGridViewCellBorderStyle.None

    '// Set the SelectionMode property
    grd.SelectionMode =3D
    DataGridViewSelectionMode.FullRowSelect

    '// Set the AutoSizeColumnsMode property
    'grd.AutoSizeColumnsMode =3D
    DataGridViewAutoSizeColumnsMode.AllCells

    Catch OleDbException As OleDbException
    MessageBox.Show(OleDbException.Message)
    End Try

    Hope your problem get resolved.

  5. #5
    Join Date
    May 2008
    Posts
    913

    Re: How to load datagrid into dataset in vb.net

    For loading datagrid into dataset you have to write a proper code for that. I think you have forgot to write some code. Whenever the program starts, it composes a connect string and uses it to create two OleDb Adapters, one to load the Addresses table and one to load the TestScores table. It then uses the adapters' Fill methods to load data into a DataSet. It sets the DataGrid's DataSource property to the DataSet and the DataGrid automatically displays the data.
    Private m_da_Addresses As OleDbDataAdapter
    Private m_da_TestScores As OleDbDataAdapter
    Private m_DataSet As DataSet

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Const SELECT_ADDRESSES As String = "SELECT * FROM " & _
    "Addresses"
    Const SELECT_TEST_SCORES As String = "SELECT * FROM " & _
    "TestScores"

    ' Get the database file name.
    Dim db_name As String = Application.StartupPath
    db_name = db_name.Substring(0, db_name.LastIndexOf("\"))
    db_name &= "\Contacts.mdb"

    ' Compose the connection string.
    Dim connect_string As String = _
    "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & db_name & ";" & _
    "Persist Security Info=False"

    ' Create a DataAdapter to load the Addresses table.
    m_da_Addresses = New OleDbDataAdapter(SELECT_ADDRESSES, _
    connect_string)

    ' Create a DataAdapter to load the Addresses table.
    m_da_TestScores = New _
    OleDbDataAdapter(SELECT_TEST_SCORES, connect_string)

    ' Create and fill the DataSet.
    m_DataSet = New DataSet
    m_da_Addresses.Fill(m_DataSet, "Addresses")
    m_da_TestScores.Fill(m_DataSet, "TestScores")

    ' Bind the DataGrid to the DataSet.
    dgContacts.DataSource = m_DataSet
    End Sub

    When the program is ending, it makes an OleDbCommandBuilder object for the adapters. The adapters will use the command builder to make SQL insert, update, and delete commands as necessary to save any changes to the database.
    The program then uses the adapters' Update methods to save any changes to the data.

    ' Save changes to the data.
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e _
    As System.ComponentModel.CancelEventArgs) Handles _
    MyBase.Closing
    ' Use a CommandBuilder to make the INSERT,
    ' UPDATE, and DELETE commands as needed.
    Dim command_builder As New _
    OleDbCommandBuilder(m_da_Addresses)

    ' Update the database.
    Try
    m_da_Addresses.Update(m_DataSet, "Addresses")
    m_da_TestScores.Update(m_DataSet, "TestScores")
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try
    End Sub

    Try to execute this code, hope your problem will get solved.

  6. #6
    Join Date
    Nov 2009
    Posts
    792

    Re: How to load datagrid into dataset in vb.net

    I think You have to use bindgrid method. This method contain some code which you have to use:
    Sub BindGrid()
    Dim myConnection as New SqlConnection (strConn)
    Dim DS1 As DataSet
    Dim DS1 As DataSet
    Dim MyCommand As SqlDataAdapter
    MyCommand = new SqlDataAdapter("exec s_get_table1", MyConnection)
    DS1 = new DataSet()
    MyCommand.Fill(DS1, "Table1")
    MyCommand = new SqlDataAdapter("exec s_get_table2", MyConnection)
    DS2 = new DataSet()
    MyCommand.Fill(DS2, "Table2")
    The above code is the example of bindgrid method.

Similar Threads

  1. Dataset vs DataReader in .Net
    By Vireshh in forum Software Development
    Replies: 3
    Last Post: 07-05-2011, 11:32 AM
  2. How to create a new DataSet
    By Leonard in forum Windows Software
    Replies: 5
    Last Post: 23-03-2010, 08:52 AM
  3. How to use dataset in ASP.Net?
    By Roxy_jacob in forum Software Development
    Replies: 3
    Last Post: 28-11-2009, 06:13 PM
  4. How is DataSet.xsd used in any particular website
    By Guns-n-Roses in forum Software Development
    Replies: 3
    Last Post: 25-09-2009, 12:52 PM
  5. Working with XML and DataSet
    By Aanand in forum Software Development
    Replies: 3
    Last Post: 01-05-2009, 02:38 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,878,867.04655 seconds with 17 queries