Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

datagridview checkbox endedit

Posted on 2008-10-08
3
Medium Priority
?
2,373 Views
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??  
0
Comment
Question by:pprater1
[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
  • 2
3 Comments
 
LVL 5

Expert Comment

by:harwantgrewal
ID: 22675514
Hi
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.

Thanks

Harry
0
 

Accepted Solution

by:
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
        LoadForm()
    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")
        Else
            MessageBox.Show("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
 
        grid.EndEdit()
 
        dtChanges = mdv.Table.GetChanges
 
        If dtChanges Is Nothing Then
            MessageBox.Show("No Changes")
        Else
            MessageBox.Show("Changes")
        End If
    End Sub
 
    Private Sub LoadForm()
        Dim dt As New DataTable
        Dim dr As DataRow
 
        dt.Columns.Add("BooleanColumn", GetType(Boolean))
        dt.Columns.Add("StringColumn")
 
        dr = dt.NewRow
        dr(0) = True
 
        dt.Rows.Add(dr)
        dt.AcceptChanges()
 
        mdv = New DataView(dt, "", "", DataViewRowState.CurrentRows)
 
        grid.DataSource = mdv
    End Sub

Open in new window

0
 
LVL 5

Assisted Solution

by:harwantgrewal
harwantgrewal earned 400 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
            button1.Focus();
            grid.EndEdit();
 
            dtChanges = mdv1.Table.GetChanges();
 
            if (dtChanges == null)
            {
                MessageBox.Show("No Changes");
            }
            else
            {
                MessageBox.Show("Changes");
            }
        }

Open in new window

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

722 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