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

datagridview checkbox endedit

Posted on 2008-10-08
Last Modified: 2013-11-07
I have what is in effect 2 different issues that I believe must be related because the technique that I am using is the same.  I have a simple form with a simple databound DataGridView.  One of the columns of the datasource (which is a datatable from dataset) is a Bit column and therefore renders as a checkbox column in the grid (which is desired).  My save procedure involves getting the changes (GetChanges) from the datatable and passing these changes to a routine that uses a SqlDataAdapter to apply the changes to a SQL database.  If I take the steps of 1) Loading the DataGridView with data 2) Checking (or unchecking) the one of the checkboxes and 3) immediately save without losing focus of the field -- when I try to GetChanges on my datatable, there are no changes.  If I make the change and then move to a different row and save, my GetChanges call has the changes.  

In my save logic, before I call the GetChanges procedure against my datatable, I am automatically calling the EndEdit procedure against the DataGridView.  EndEdit gets called everytime.  This works for all fields except for the checkbox field.  For example, if instead of checking/unchecking the checkbox, I edit a text field, this method works just fine and the changes are evident in my GetChanges call.  

Another strange thing that I have noticed is that the new row that I enter data into acts the same way.  I have left the option to allow users to add rows as default which is True.  Therefore the bottom row in the DataGridView is empty with an asterisk in the row header.  When I enter data here and again immediately save without going to a different row, the changes are not commited which is evident after the call to GetChanges against the datasource.  Again, before the call to the GetChanges procedure is made, I am calling the EndEdit procedure which I expect to commit the changes to the datasource.  Also I have noticed that when I edit the bottom row to add a new row, then in my Save procedure call IsNewRow against the ActiveRow of the DataGridView (DataGridView.ActiveRow.IsNewRow) the value is False - I would expect it to be true.

I can attach code tomorrow.

What am I doing wrong here??  
Question by:pprater1
  • 2

Expert Comment

ID: 22675514
Its really hard for me to understand the problem. Is there any JavaScript in the file which runs on any of these actions? Another thing are you using any Ajax in that page as I have noticed that AJAX is one of the biggest cause of most of the problems.



Accepted Solution

pprater1 earned 0 total points
ID: 22679495
To answer your question, no, there is no Javascript or AJAX as this is a Windows Forms application.

Wow, just made a very interesting discovery.  If I use a regular Windows Button on my form to initiate the Save routine, it works, even without the EndEdit() call against the DataGridView.  I am currently using a ToolStrip with a "Save" ToolStripButton.  So evidently the problem lies in using a ToolStripButton instead of a regular Windows Button.

What is that all about?

In the code snippet is the code of a test form to illustrate.  In a blank Windows Application project, add to a form 3 things 1) ToolStrip with 1 "Save" ToolStripButton 2) 1 regular Windows "Save" Button and 3) a DataGridView.  Use the code snippet below in your class code and rename where necessary and run.  When it loads, uncheck the BooleanColumn in the first row.  Press the "Save" button in the tool bar.  It should say "No Changes".  Now press the Reset Form button to reset the form, uncheck the BooleanColumn in the first row again and press the regular Windows "Save" button.  It should say "Changes".  
 Private mdv As DataView
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim dtChanges As DataTable
        dtChanges = mdv.Table.GetChanges
        If dtChanges Is Nothing Then
            MessageBox.Show("No Changes")
        End If
    End Sub
    Private Sub ToolStripButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Dim dtChanges As DataTable
        dtChanges = mdv.Table.GetChanges
        If dtChanges Is Nothing Then
            MessageBox.Show("No Changes")
        End If
    End Sub
    Private Sub LoadForm()
        Dim dt As New DataTable
        Dim dr As DataRow
        dt.Columns.Add("BooleanColumn", GetType(Boolean))
        dr = dt.NewRow
        dr(0) = True
        mdv = New DataView(dt, "", "", DataViewRowState.CurrentRows)
        grid.DataSource = mdv
    End Sub

Open in new window


Assisted Solution

harwantgrewal earned 100 total points
ID: 22683272
Hi pprater1

I just did a test windows app and I found somthing which is strange to me and I dont have any written proof. But I think when you click the button in form it takes focus away from grid do the click even and then get the focus back on the grid cell. But when you click the toolstipbutton it performs the click and dosen't remove the focus from the grid. But when I do the change and before click on the toolstrip button I just change the focus by clicking on other cell and then click the toolstip button it works. and I tried to achive this by moving the focus away from grid by putting button1.Focus(); in the toolstipbutton click and it worked again.

PS: I am working with C#
private void toolStripButton1_Click(object sender, EventArgs e)
            DataTable dtChanges;
            //MY CHANGE
            dtChanges = mdv1.Table.GetChanges();
            if (dtChanges == null)
                MessageBox.Show("No Changes");

Open in new window


Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

837 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