Solved

datagridview checkbox endedit

Posted on 2008-10-08
3
2,346 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
  • 2
3 Comments
 
LVL 5

Expert Comment

by:harwantgrewal
Comment Utility
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
Comment Utility
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 100 total points
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now