Link to home
Start Free TrialLog in
Avatar of jackjohnson44
jackjohnson44

asked on

dataadapter datagridview

I want to fill a datagridview, enter data into it, then save the data back to the database.
It should be pretty simple, but I can't get it to work.

I was using another post, but all the comments were getting cluttered and no one was answering the question.  It is pretty important, I have been struggling for days.  If you want to see the other question, it is here: https://www.experts-exchange.com/questions/22044293/vb-net-dataadapter-update-and-datagridview.html

select * from users
ID (auto increment), username, password
Avatar of Kudzullc
Kudzullc

Do you need to specify Headers?

If not, do this....


Dim myTable As New DataTable
Dim cmdFillForm As New OleDb.OleDbDataAdapter("select * from users ID (auto increment), username, password", onnectionOleDB)
cmdFillForm.Fill(myTable)
dgViewUsers.DataSource = myTable

dgViewUsers being the drag and drop datagridview component.

This will simply display the select in a datagridview.  Very simple and can be modified.  You can also label static information for the column headers.  play around with this, then modify.

gl,
Lucas
Avatar of jackjohnson44

ASKER

thanks, but my problem was mostly with updating the database after tying something into my dataview.  Can you please show how to do that?
ahhh, i always do this...  i see the issue i the other post and i completely missed the idea of the post.  to be completely honest, i would only use datagrid for viewing only and create totally separate small forms to add data and simply refresh on me.close.
I thought the datagrids were made to do what I am saying.
They have the data members to do this, yes, but i would programatically move around this functionality.  The only reason i could think of someone needing to have this access is in an admin situation.  If users are allowed this funationality then the data is vunerable and may start showing anomalies based on inconstent data when 'many' users access this at the same time.  I have not read the entire thread in other forum and didnt read the 'save back to database" part in the original post.  you have many experts following through and they should be able to provide extensive answers as well as advice.

When i posted, i just knew how to fill a datagridview and manipulate the data going in.  That's all!  Once upon a time, I tinkered around with the functionailty of the Datagrid members.  Now i do manual builds and the data is manipulated using forms.

sry&gl,
Lucas
thanks

so you would display data in the grid and enter it in a different way?
jj,

sure.  i would set the datagridview to readonly and only allow them to search thru refreshing (dynamic sql build) and create a seperate form to add, modify and delete.  That way you control the data more effeciently and less oppurnity for mistakes.  i have done it like this many times.

Lucas
thanks, one more quick question:

Why did you specifu the auto increment?
Does this do anything to the dataset?

select * from users ID (auto increment), username, password
jj,

i think binding the data to a form is going to be you best bet. If you have trouble doing this, then read through the link below...

http://www.startvbdotnet.com/ado/simplebinding.aspx

It is a very good discussion/tutorial on binding data to a form.  Once you have the form, simply put a button on the form to fill a datagridview.  

OR, lol just thought of this.  You can set a refresh routine so that on the bottom of the form, below your binded data, a datagridview could be present.  Everytime you update, insert or delete, you can refresh the datagrid to give realtime views.

Lucas
I just copied the SQL string you had in original post.  Thats all!

Lucas
Should have been

"select  ID , username, password from users"
Dim cm As CurrencyManager

    Private Sub yourForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        yourDataAdapter.Fill(yourDataSet, "yourTable")
        yourDataGrid.DataSource = yourDataSet.Tables("yourTable")

        cm = CType(BindingContext(yourDataSet.DataSource), CurrencyManager)
        Dim cb As Oledb.oledbCommandBuilder = New oledb.oledbCommandBuilder(yourDataAdapter)
     
       
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
       yourDataAdapter.Update(yourDataSet, "yourTable")
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        cm.EndCurrentEdit()
        cm.RemoveAt(cm.Position)
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        cm.EndCurrentEdit()
        cm.AddNew()
    End Sub


ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Mine won't update!