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
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
  • 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

615 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