Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Grid View Cell Edit

Posted on 2006-11-07
5
Medium Priority
?
1,712 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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Screencast - Getting to Know the Pipeline
Suggested Courses

885 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