Link to home
Start Free TrialLog in
Avatar of running32
running32

asked on

Updating Datagrid

I have a datagird in my windows form.   l'm having problems updating the dataset and writing back to the database.   On a mousedown event I want to add the username and date.  This works.   I then need to write in some comments then update the datagrid.   My problem is that it will not update.

 pt = New Point(e.X, e.Y)
        Dim testdate As String
       Dim hti As DataGrid.HitTestInfo = DataGrid2.HitTest(pt)
        If hti.Type = DataGrid.HitTestType.Cell Then
            DataGrid2.CurrentCell = New DataGridCell(hti.Row, hti.Column)
            DataGrid2.Select(hti.Row)
            DataGrid2.Item(hti.Row, 2) = UserName 'Column 3
            DataGrid2.Item(hti.Row, 1) = Now 'Column 3

        End If
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

How are you attempting to update the database?

Bob
Avatar of running32
running32

ASKER

on form leave I was  was running the command

  SqlDataAdapter4.Update(Dshhnotes1)
   Dshhnotes1.AcceptChanges()
 If Dshhnotes1.HasChanges Then
            SqlDataAdapter4.Update(Dshhnotes1.Tables("tblHouseholdnote"))
            Dshhnotes1.AcceptChanges()
        End If

When I add this to the on leave it does not reconize a change.   how can I get it to see I have added to the datagrid.

Thanks
It will let me update any existing records but will not let me add any new records to the dataset.
1) Are you getting any exceptions?

2) Do you have an InsertCommand defined for the SqlDataAdapter?

Bob
I am using a vs2003 sqldataAdapter.  I checked the updates, insert commands and they are all ok.  

No I am not getting any exceptions unfortunately just does not update.  :-(

Thanks
Check to see if you have changes:

Dim changes AS DataSet = Dshhnotes1.GetChanges()

If Not changes Is Nothing Then
   Console.WriteLine("Tables:  " & changes.Tables.Count)
   For Each table As DataTable in changes
      Console.WriteLine(table.Rows.Count)
   Next table
End If

Bob
When I try and add this to my code I get the error on "in changes" it states it is not a collection type.

Thank you
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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
Here is the code I am trying to work with.   The records will update ok but I cannot add any new ones.
Thanks for your help.  When I test to see if there is a change it always comes back  as I for row count even if I have 4 rows.  Thanks


**************Here is where I assign the datasource and formate the datagrid.
Dshhnotes1.Clear()
        SqlDataAdapter4.SelectCommand.Parameters("@paramhh").Value = lnghouseid
        SqlDataAdapter4.Fill(Dshhnotes1)

        DataGrid2.DataSource = Dshhnotes1
        DataGrid2.DataMember = "tblhouseholdnote"
        Me.DataGrid2.SetDataBinding(Dshhnotes1, "tblHouseholdnote")
        Dim tablestyle1 As New DataGridTableStyle
        tablestyle1.MappingName = Dshhnotes1.Tables(0).TableName

        'Dim Data1a As New DataGridTextBoxColumn

        Dim MemoTextCol As New DataGridMemoColumn
        MemoTextCol.MappingName = "memeligibility"
        MemoTextCol.HeaderText = "Memo"
        MemoTextCol.NullText = ""
        MemoTextCol.Width = 255
        tablestyle1.GridColumnStyles.Add(MemoTextCol)

        Dim Data2a As New DataGridTextBoxColumn
        Data2a.MappingName = "dtmcreate"
        Data2a.HeaderText = "Created"
        Data2a.Width = 75
        tablestyle1.GridColumnStyles.Add(Data2a)

        Dim Data3a As New DataGridTextBoxColumn
        Data3a.HeaderText = "Created By"
        Data3a.MappingName = "strcreate"
        Data3a.Width = 95
        tablestyle1.GridColumnStyles.Add(Data3a)

        Dim Data4a As New DataGridTextBoxColumn
        Data4a.HeaderText = "Created By"
        Data4a.MappingName = "lnghouseid"
        Data4a.Width = 20
        tablestyle1.GridColumnStyles.Add(Data4a)
        Me.DataGrid2.TableStyles.Add(tablestyle1)


**********  Here is where I assign some values to the datagrid
  Private Sub DataGrid2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid2.MouseUp
pt = New Point(e.X, e.Y)
        Dim testdate As String

        Dim hti As DataGrid.HitTestInfo = DataGrid2.HitTest(pt)
        If IsDBNull(DataGrid2.Item(hti.Row, 0)) = True Then
            If hti.Type = DataGrid.HitTestType.Cell Then
                DataGrid2.CurrentCell = New DataGridCell(hti.Row, hti.Column)
                DataGrid2.Select(hti.Row)

                DataGrid2.Item(hti.Row, 1) = Now 'Column 2
                DataGrid2.Item(hti.Row, 2) = UserName 'Column 3
                DataGrid2.Item(hti.Row, 3) = lnghouseid 'Column 4


            End If
        End If

***********************  On Datagrid on leave I update the dataset
    Me.BindingContext(Dshhnotes1.Tables(0)).EndCurrentEdit()
        SqlDataAdapter4.Update(Dshhnotes1.Tables(0))
        Dshhnotes1.AcceptChanges()