Solved

Grid View Cell Edit

Posted on 2006-11-07
5
1,672 Views
Last Modified: 2010-08-05
I have added some code to the CellEndEdit event of a Data Grid View. If I modify the value of a cell in the grid view and click on the next line, the code updates the remote database correctly. However, if I modify the value of a cell in the grid view and move to the next cell of the same row, although the CellEndEdit is triggered, the data is not modified in the remote database, and the datasource's HasChanges value is still False.

How can I fix it, so when I change the value of a cell, the dataset and the remote database get updated?
0
Comment
Question by:huji
  • 3
  • 2
5 Comments
 
LVL 34

Expert Comment

by:Sancler
ID: 17894347
A DataGridView updates its own version of any row/record as soon as the edit on any of its cells is ended.  But a changed row/record is only passed back to the datasource datatable when the edit on the row is ended.  This (as you have seen) happens automatically if the user changes to a different row.  Otherwise, you have to make it happen by calling .EndEdit on the DataGridView's .BindingSource, if it has one, or .EndCurrentEdit on the binding manager of its .DataSource.  Is that enough for you to work out what extra code you need?  If not, can you please post the code by which you bind your datatable to your datagridview?

Roger
0
 
LVL 14

Author Comment

by:huji
ID: 17899598
Roger,
My DGV is not using a DataSource, but a DataSet is manually connected to it in my code. Here is the code of the function which is supposed to handle DGV changes:

    Private Sub dgvTeachers_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTeachers.CellEndEdit
        If dsTeachers.HasChanges Then MsgBox("A")
        frmMain.connWorkshop.Open()
        Dim strSQL As String = "select ID, FName ,LName from tblTeachers"
        Dim daTeachers As New SqlDataAdapter(strSQL, frmMain.connWorkshop)
        daTeachers.TableMappings.Add("Table", "tblTeachers")
        Dim command_builder As New SqlCommandBuilder(daTeachers)
        daTeachers.ContinueUpdateOnError = False
        daTeachers.Update(dsTeachers)
        frmMain.connWorkshop.Close()
        Me.RefreshForm()
    End Sub

Please make the changes and post it back.
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17899843
>>
My DGV is not using a DataSource, but a DataSet is manually connected to it in my code
<<

What I want to know is what is the code that does that "manual" connection.  I imagine it will be something like

    dgvTeachers.DataSource = dsTeachers.Tables("tblTeachers")

but, ideally, the code we need for committing a newly edited row from the dgv back to the datatable ought to use the same syntax.  That's why I'd like to see the actual code you use to create the link.

Roger
0
 
LVL 14

Author Comment

by:huji
ID: 17900146
Here you go:

        frmMain.connWorkshop.Open()
        Dim strSQL As String = "select ID, FName ,LName from tblTeachers"
        Dim daTeachers As New SqlDataAdapter(strSQL, frmMain.connWorkshop)
        daTeachers.TableMappings.Add("Table", "tblTeachers")
        dsTeachers.Reset()
        daTeachers.Fill(dsTeachers)
        With dgvTeachers
            .DataSource = dsTeachers.Tables("tblTeachers")
            .Columns(0).ReadOnly = True
            .Columns(0).HeaderText = ""
            .Columns(0).Width = 30
            .Columns(1).HeaderText = "Name"
            .Columns(2).HeaderText = "Surname"
            .Refresh()
        End With
        frmMain.connWorkshop.Close()
0
 
LVL 34

Accepted Solution

by:
Sancler earned 400 total points
ID: 17900289
Put this as the first line of your sub dgvTeachers_CellEndEdit

   BindingContext(dsTeachers.Tables("tblTeachers")).EndCurrentEdit

Roger
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now