5 Replies - 1236 Views - Last Post: 01 March 2012 - 11:38 PM Rate Topic: -----

#1 dev15mech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-February 12

Can i add a datagrid view in the form through code

Posted 29 February 2012 - 03:47 AM

hi

i need some info. i want to add a data grid view through vb.net. and when i enter some data in that it will updated in the access database.
Is This A Good Question/Topic? 0
  • +

Replies To: Can i add a datagrid view in the form through code

#2 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Can i add a datagrid view in the form through code

Posted 29 February 2012 - 03:49 AM

sure you can.
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1727
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Can i add a datagrid view in the form through code

Posted 29 February 2012 - 05:24 AM

and here's a tutorial on the subject. Not specifically a dgv, but the concept is the same, just a little more work adding columns and such.
Was This Post Helpful? 0
  • +
  • -

#4 dev15mech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-February 12

Re: Can i add a datagrid view in the form through code

Posted 01 March 2012 - 03:43 AM

View Postdev15mech, on 29 February 2012 - 03:47 AM, said:

hi

i need some info. i want to add a data grid view through vb.net. and when i enter some data in that it will updated in the access database.



Hi i have created a code by which i am able to add a table to mdb file in run time and then using it to show up the value in datagridview. but now i want to update the changes made in the data gridview to mdb file. how to do it. do i have to read one by one data and then insert in to mdb file or is there any direct method available.

Refer my code below:

' for accessing access mdb file in data grid
'===============================================

 sql = "SELECT * FROM RFOS00" & CmbRFOSList.Text
        DBda = New OleDb.OleDbDataAdapter(sql, DBcon)
        DBda.Fill(DBds, "RFOSIndvidual")

        DataGridRFOS.DataSource = DBds
        DataGridRFOS.DataMember = "RFOSIndvidual"

       
'Save/update data inputted by user in datagrid view to mdb
'=============================================================

   Dim bindingSource As New BindingSource()
        Dim table As New DataTable()

        Dim newcb As New OleDb.OleDbCommandBuilder(DBda)


        DBda.Fill(table)
        bindingSource.DataSource = table
     
        DBds.Tables("RFOSIndividual").AcceptChanges()
        DBda.Update(DBds,"RFOSIndividual")


MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 01 March 2012 - 03:52 AM

Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1727
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Can i add a datagrid view in the form through code

Posted 01 March 2012 - 05:19 AM

The basics of this is once the dgv is populated, you can make changes. At this point you are changing the dataset. Now since you are selecting all columns, the changes can be done fairly easy.

Of course you will have to put this somewhere so that it can be processed with each change to the datasource. Something like a save button. You will also need to change the scope of your objects so that they are accessible in any other subroutines.

dim DBcb as new OleDb.OleDb.CommandBuilder(DBda)' again since you selected all columns, this can build the proper statment(ie, update or insert)
DBda.Update(DBds, "RFOSIndividual")


Also, in the future, please start a new topic when the subject of the topic changes. This will help others in your situation find any solutions easier. :)

This post has been edited by CharlieMay: 01 March 2012 - 05:24 AM

Was This Post Helpful? 0
  • +
  • -

#6 dev15mech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-February 12

Re: Can i add a datagrid view in the form through code

Posted 01 March 2012 - 11:38 PM

View PostCharlieMay, on 01 March 2012 - 05:19 AM, said:

The basics of this is once the dgv is populated, you can make changes. At this point you are changing the dataset. Now since you are selecting all columns, the changes can be done fairly easy.

Of course you will have to put this somewhere so that it can be processed with each change to the datasource. Something like a save button. You will also need to change the scope of your objects so that they are accessible in any other subroutines.

dim DBcb as new OleDb.OleDb.CommandBuilder(DBda)' again since you selected all columns, this can build the proper statment(ie, update or insert)
DBda.Update(DBds, "RFOSIndividual")


Also, in the future, please start a new topic when the subject of the topic changes. This will help others in your situation find any solutions easier. :)



Sorry for the Wrong Posting, i was in a thought that it will help others what will happen to my query..

BTW i have tried the things asked by you, but it still not updating the value in the database.

here is my code:(For saving the data)


Dim DBdaforNewRFOS As OleDb.OleDbDataAdapter
Dim DBdsforNewRFOs As New DataSet

Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


Try
Dim newcb As New OleDb.OleDbCommandBuilder(DBda)
DataGridRFOS.EndEdit()
DBdsforNewRFOs.AcceptChanges()
DBdaforNewRFOS.Update(DBdsforNewRFOs, "RFOSIndvidual")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try


End Sub
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1