?
Solved

Grid View Cell Edit

Posted on 2006-11-07
5
Medium Priority
?
1,702 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1600 total points
ID: 17900289
Put this as the first line of your sub dgvTeachers_CellEndEdit

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

Roger
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

719 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