Updating a DataBound DataGridView & Database

Hi,

I’m experimenting with (and trying to learn) VB 2005 Express, but I can’t find a way to programmatically enter data into a data bound (bound to Main1database) DataGridView.

Basically, I have one edit box on a dialog box with the DataGridView control and a button. The user enters some a sentence into the edit box then clicks the button. The button-click event is supposed to add the sentence to the DataGridView and then update the database so it’s saved.

I’ve spent hours playing with code but can’t find a way of doing any of this – I’m a complete novice to VB Express!

Could some one provide me with the majority/all of the code that would achieve this – not just a snippet! (I’ve tried using various code snippets but none seem to work.)

Many, many thanks!

Brad



Answer this question

Updating a DataBound DataGridView & Database

  • Jodney

    Thanks! That works great!

    Just one more question, if you got time.

    When the user clicks the button, a new row needs to be added to the end of the database table, and then I obviously add the sentence to the new cell (thanks to you I can now add the sentence). But how I create a new row and insert the sentence at the correct index (row, column). (How do I get the index of the new row )

    I appreciate your response – you’ve saved me many more hours of experimenting.

    Cheers,

    Brad.


  • new2dev

    Many thanks again! It now works great!

    It's amazing how a little bit of help can save you hours of frustration.

    Cheers!

    Brad.


  • DJ2ply

    hi,

    lets say you have a data tabe dt , that contains 2 columns



    Private Sub AddRowToTable(ByVal val1 As String, ByVal val2 As String)
    Dim dr As DataRow = dt.NewRow
    dr(
    "Column1") = val1
    dr(
    "Column2") = val2
    dt.Rows.Add(dr)
    End Sub
    'you can call this sub like

    AddRowToTable("something","otherthing")

    hope this helps



  • gladwin

    hi,

    you can do something like this



    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' if you use inputbox or formdialog enter the code for it first
    'and replace the textbox1.text by your string
    dataGridView1.BeginEdit(true)
    'you can select a cell by its coordinates (row,column)indexs
    dataGridView1.Rows(1).Cells(1).Value = textBox1.Text
    dataGridView1.EndEdit
    'update your database from your dataset
    Me.MyTableAdapter.Update(Me.MyDataSet.MyTable)
    End Sub

    hope this helps



  • Updating a DataBound DataGridView & Database