Solved

Grid View Cell Edit

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

632 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