473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Datagridview doesn't update current row values

VB2008
I have a DataGridView with MultiSelect = True and
SelectionMode=FullRowSelect. One of the columns is a checkbox column. I have
a function that goes through all the selected rows and sets the value of
this column.

For Each row In DataGridView1.SelectedRows
row.Cells("MyCheckboxColumn").Value = False
Next

This seems to almost work, but I noticed that sometimes it does not update
the first or the last selected row. On investigation, it seems that the
value is updated, but if the row happens to be the currentrow, the change is
not displayed until the currentrow is changed. I have managed to get around
this problem by setting CurrentCell = Nothing after I have made the changes,
but this has the undesirable side effect of clearing the selection.

Any suggestions?

TIA
Phil.

Jun 27 '08 #1
20 20247
Hi Phil,

I performed a test based on your description but didn't reproduce the
problem on my side. All selected rows in the DataGridView are updated
immediately in my test application.

I create a WinForm Application project in VS2008 and add a DataGridView and
a Button on the Form1. I add a DataGridViewCheckBoxColumn in the
DataGridView. The following is the code in the Form1.vb:

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.DataGridView1.SelectionMode =
DataGridViewSelectionMode.FullRowSelect
Me.DataGridView1.RowCount = 10
For i As Integer = 0 To 9
Me.DataGridView1.Rows(i).Cells(0).Value = True
Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
For Each row As DataGridViewRow In Me.DataGridView1.SelectedRows
row.Cells(0).Value = False
Next
End Sub
End Class

Build and run the application. Select some rows in the DataGridView with
Mouse and click the Button, all selected rows are updated immediately.
Is there any difference between your code and mine?

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #2

"Linda Liu[MSFT]" <v-****@online.microsoft.comwrote in message
news:Fj**************@TK2MSFTNGHUB02.phx.gbl...
Hi Phil,

I performed a test based on your description but didn't reproduce the
problem on my side. All selected rows in the DataGridView are updated
immediately in my test application.
Thanks for looking at this. My datagridview is part of a larger application,
so there is quite a lot of difference from your code. I shall try to
reproduce the problem in a smaller app. The main thing that springs to mind
that is different is that my data is populated from a datasource (OleDb -
access/jet). I'm not sure if that is relevant.

Phil.
Jun 27 '08 #3

"Linda Liu[MSFT]" <v-****@online.microsoft.comwrote in message
news:Fj**************@TK2MSFTNGHUB02.phx.gbl...
Hi Phil,

I performed a test based on your description but didn't reproduce the
problem on my side. All selected rows in the DataGridView are updated
immediately in my test application.
OK. I have managed to adapt your code to reproduce the problem.
Create the datagridview initially with no columns and replace Form_Load
as follows:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

Dim data As New DataTable
data.Columns.Add("Column1", Type.GetType("System.Int16"))
For i As Integer = 0 To 9
data.Rows.Add(1)
Next

Me.DataGridView1.DataSource = data

Dim col As New DataGridViewCheckBoxColumn
Me.DataGridView1.Columns.Add(col)
col.DataPropertyName = "Column1"

End Sub

Jun 27 '08 #4
OK. I have managed to adapt your code to reproduce the problem.
Create the datagridview initially with no columns and replace Form_Load
as follows:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Me.DataGridView1.SelectionMode =
DataGridViewSelectionMode.FullRowSelect

Dim data As New DataTable
data.Columns.Add("Column1", Type.GetType("System.Int16"))
For i As Integer = 0 To 9
data.Rows.Add(1)
Next

Me.DataGridView1.DataSource = data

Dim col As New DataGridViewCheckBoxColumn
Me.DataGridView1.Columns.Add(col)
col.DataPropertyName = "Column1"

End Sub
It seems the problem occurs when there are two columns bound to the same
data property.
If I add:
Me.DataGridView1.Columns.Remove("Column1")
The problem does not occur.

In my main application I hide this column. I shall try removing it instead,
and see if that fixes it.
It does seem to be a bug in the datagridview data binding though.
Jun 27 '08 #5
In my main application I hide this column. I shall try removing it
instead,
and see if that fixes it.
It didn't fix it :-(
Not sure why, perhaps because there are other columns too.
Jun 27 '08 #6
It seems the problem occurs when there are two columns bound to the same
data property.
The reason I am doing this, is because the column does not automatically
appear as a checkbox. The underlying SQL query is of the following form:

SELECT EXISTS(subquery), ....

For some reason the result of the EXISTS function seems to come back as an
integer in the Dataset. If there was some way to make this return a boolean,
that might solve my problem. This is OleDb (access/jet).
Jun 27 '08 #7
Hi Phil,

Thank you for your prompt reply!

I performed a test based on your sample code and did reproduce the problem
on my side. If I update the data in the data source in code directly, the
problem also exists even if there's only one DataGridViewCheckBoxColumn in
the DataGridView.

This problem is not relevant to multiple columns' being bound to the same
data property.

The reason behind this problem is that data binding doesn't refresh data of
the current item in UI when the data in the underlying data source are
changed.

The workaround is to call the EndCurrentEdit method of the corresponding
BindingManagerBase object associated with the data source to end current
edit to the data source and refresh the UI.

For example, after you change the value of the DataGridViewCells or change
the data in the data source directly, add the following line of code:
Me.BindingContext(data).EndCurrentEdit()
For some reason the result of the EXISTS function seems to come back as
an integer in the Dataset.

The EXISTS operator returns a value of bit. In .NET, the corresponding type
is Boolean. If a data column is of type Boolean, DataGridView will generate
a DataGridViewCheckBoxColumn for this data column.

Is the DataTable strong-typed in your application? If so, you may open the
DataSet in the designer and select the column of the DataTable and check
the column's type in the Properties window. If not, you need to set a break
point in your code and press F5 to run the application. When the
application enters into break mode, you can check the type of the
DataTable's column.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #8
Hi Phil,

How about the problem now?

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #9
How about the problem now?

Hello Linda,

Thanks for your reply. I was away on holiday last week, and am just catching
up with messages. I shall take a look in more detail later.

Thanks,
Phil.
Jun 27 '08 #10
I performed a test based on your sample code and did reproduce the problem
on my side. If I update the data in the data source in code directly, the
problem also exists even if there's only one DataGridViewCheckBoxColumn in
the DataGridView.
OK.
>
This problem is not relevant to multiple columns' being bound to the same
data property.
OK. That was just a theory :-)
>
The reason behind this problem is that data binding doesn't refresh data
of
the current item in UI when the data in the underlying data source are
changed.
OK. That makes sense I think. It still thinks the row is being edited, so
doesn't refresh until the current row is changed.
>
The workaround is to call the EndCurrentEdit method of the corresponding
BindingManagerBase object associated with the data source to end current
edit to the data source and refresh the UI.
OK.
>
For example, after you change the value of the DataGridViewCells or change
the data in the data source directly, add the following line of code:
Me.BindingContext(data).EndCurrentEdit()
I'll try that. Thanks.
>
>For some reason the result of the EXISTS function seems to come back as
an integer in the Dataset.

The EXISTS operator returns a value of bit. In .NET, the corresponding
type
is Boolean. If a data column is of type Boolean, DataGridView will
generate
a DataGridViewCheckBoxColumn for this data column.

Is the DataTable strong-typed in your application?
No.
If so, you may open the
DataSet in the designer and select the column of the DataTable and check
the column's type in the Properties window. If not, you need to set a
break
point in your code and press F5 to run the application. When the
application enters into break mode, you can check the type of the
DataTable's column.
I think I have done this, and IIRC it was returning Int16. I'll check this
again tomorrow.
>
Thanks for your help.
Phil.
Jun 27 '08 #11
>
>For example, after you change the value of the DataGridViewCells or
change
the data in the data source directly, add the following line of code:
Me.BindingContext(data).EndCurrentEdit()

I'll try that. Thanks.
>>
BindingContext(MyDataGridView.DataSource).EndCurre ntEdit()
That did the trick.
Thanks
Phil.
Jun 27 '08 #12
Hi Phil,

Thank you for your feedback! I'm glad to hear that the problem is solved
now.

If you have any other questions in the future, please don't hesitate to
contact us. It's always our pleasure to be of assistance!

Have a good day!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #13
Thank you for your feedback! I'm glad to hear that the problem is solved
now.
Unfortunately, I've just discovered a side-effect of this fix which is going
to cause me some problems :-(
I have some code which checks whether any changes have been made to the
data, and of course now after using EndCurrentEdit, the HasChanges property
of the Dataset now returns False. I guess I'll have to maintain my own
status flag now to do this.
Jun 27 '08 #14
Unfortunately, I've just discovered a side-effect of this fix which is
going to cause me some problems :-(
I have some code which checks whether any changes have been made to the
data, and of course now after using EndCurrentEdit, the HasChanges
property of the Dataset now returns False. I guess I'll have to maintain
my own status flag now to do this.
This seems to do the trick:

BindingContext(MyDataGridView.DataSource).EndCurre ntEdit()
MyDataGridView.DataSource.Rows(0).BeginEdit()

This forces the display to update correctly after I update the data, and
HasChanges still returns True.
Is this likely to cause any problems? This code may be called a number of
times, and I never call AcceptChanges because I am not using the DataSource
to commit the changes made to the data.
Jun 27 '08 #15
>Is the DataTable strong-typed in your application?

No.
>If so, you may open the
DataSet in the designer and select the column of the DataTable and check
the column's type in the Properties window. If not, you need to set a
break
point in your code and press F5 to run the application. When the
application enters into break mode, you can check the type of the
DataTable's column.

I think I have done this, and IIRC it was returning Int16. I'll check this
again tomorrow.
>>
Dataset.Tables(0).Columns(MyColumn).DataType:
{Name = "Int16" FullName = "System.Int16"} System.Type

MyDataSet.Tables(0).Rows(0).Item(MyColumn):
-1 {Short} Object

The sql is of the form:

SELECT ... , Exists(subquery), ....

the database is Access/Jet (mdb) and I'm using an OleDbConnection and
OleDBAdapter to execute the query and populate the Dataset.
Jun 27 '08 #16
Hi Phil,
>... now after using EndCurrentEdit, the HasChanges property of the Dataset
now returns False.

I performed a test on this but didn't reproduce the problem on my side. The
HasChanges method of the DataSet still returns true after I call the
EndCurrentEdit method on the associated BindingManagerBase object.

In fact, only after we call the DataSet.AcceptChanges method, the
HasChanges method will return false.
the database is Access/Jet (mdb) and I'm using an OleDbConnection and
OleDBAdapter to execute the query and populate the Dataset.

If you drag the database table from Server Explorer and drop it onto the
DataSet designer, what's the data type of the data column in question?

If the data column's data type is still Int16, a workaround is to change
the type to Boolean in code by yourself.

Hope this helps.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #17
Hi Phil,

How about the problem now?

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #18
>
>>... now after using EndCurrentEdit, the HasChanges property of the Dataset
now returns False.

I performed a test on this but didn't reproduce the problem on my side.
The
HasChanges method of the DataSet still returns true after I call the
EndCurrentEdit method on the associated BindingManagerBase object.
That's odd.
>
In fact, only after we call the DataSet.AcceptChanges method, the
HasChanges method will return false.
That's what I would have expected.
>
>the database is Access/Jet (mdb) and I'm using an OleDbConnection and
OleDBAdapter to execute the query and populate the Dataset.

If you drag the database table from Server Explorer and drop it onto the
DataSet designer, what's the data type of the data column in question?
I am not using the database designer in this project. I have some generic
code that I pass a SQL query to. This uses an OleDbConnection and
OleDbDataAdapter to generate a DataSet object.
>
If the data column's data type is still Int16, a workaround is to change
the type to Boolean in code by yourself.
I can perhaps try this, although I want to avoid having to create separate
classes for each of the different database queries in my project.
Jun 27 '08 #19

"Phil" <N/Awrote in message
news:p8******************************@posted.plusn et...
>>>... now after using EndCurrentEdit, the HasChanges property of the
Dataset
now returns False.

I performed a test on this but didn't reproduce the problem on my side.
The
HasChanges method of the DataSet still returns true after I call the
EndCurrentEdit method on the associated BindingManagerBase object.

That's odd.
I wonder if this is because I have two columns both bound to the same item?
Only one of them has been changed, and this might cause some confusion.
I am not too concerned, as my workaround seems OK for now.
Jun 27 '08 #20
Hi Phil,

Thank you for your reply!
I wonder if this is because I have two columns both bound to the same
item? Only one of them has been changed, and this might cause some
confusion.

No, I don't think so. You may create a new WinForm application project to
perform a test on it, if you'd like to.
I am not too concerned, as my workaround seems OK for now.
Well, I consider I can close this issue in the newsgroup now. Do you agree
with me? If you have any concern, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Wanda | last post by:
I am using vb.net and have a bounded datagrid by using SQLDataAdaptor and SQLDataSet, it does show the data when it is first loaded. But when I change the data on the datagrid, it doesn't update...
3
by: thomasp | last post by:
I know this is a vague question, but I am not sure what information to give. I am using VB2005 beta and the datagridview is pulling data from one of three tables in an Access database. For some...
1
by: JeremyGrand | last post by:
I've read what's available here, but can't seem to make this work right. I'm experimenting with components on a form, although I'd rather create the pieces & assemble them in code, but that's...
2
by: Ivan | last post by:
I have a class Foo which have two property. I have a thread that do some process and update class Foo int B. I have a datagridview on main form. this datagridview data source to this class Foo and...
7
by: John J. Hughes II | last post by:
I have a DataGridView with a TextBoxColumn. I setting the data source to a List<stringvalue in a static class. The list is filled from a background thread. So far all is fine and it works...
1
by: MFayaz | last post by:
Hello! I have to refresh datagridview when database update , IS there any way to solution that my datagridview auto refresh when any user update database. Thanks in Advance
7
by: BONES7714 | last post by:
Hello, This is my first post to any sort of forum although I've used them to learn the very little Access/VBA that I know so please forgive my ignorance. I work for the National Guard as Combat...
1
by: bharathi228 | last post by:
hello sir, iam new to windows application.iam working with datagridview in vb.net .how to update the selected row in datagridview.any one help me with this.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.