Solved

Grid View Cell Edit

Posted on 2006-11-07
5
1,679 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 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

14 Experts available now in Live!

Get 1:1 Help Now