Refresh only the CurrentRow of a DataGridView?

G

Guest

I have a DGV that is read only. When selecting a row, the item is edited in
individual controls elsewhere on the form, also bound to the same
bindingsource. The default behavior is that when the value is changed in a
textbox, it only repaints the value in the DGV cell after the textbox commits
the value to the underlying object. I want it to update the DGV cell's
display in real time. To do this, I have code in the textbox's TextChanged
event to update the underlying object's property and refresh the DGV. This
works ok. However, if there are more than a handful of records displayed,
this refresh is slow and distracting.

So two questions:
1) Am I approaching this the correct way? Is there a better way to have the
DGV reflect the changes in real time?
2)If this is the best way, is there any way to refresh/repaint only the
currently selected row?

Thanks for any ideas.

Barry
 
R

RobinS

I guess my question would be why are you doing this? It seems to me to make
more sense for the user to put in all the fields and then maybe hit a
<Save> button and it upgrades all the entries in that row at the same time.
Maybe he would like to see what the old values are before committing the
new ones?

If you are committed to this course of action, my question is when you say
this:

what do you mean? Are you forcing an EndEdit() on the binding source, or a
Validate() or do you mean actually updating the data source?

Seems to me you might be able to accomplish this by databinding the same
data source to the same binding source. So you have a table that is data
bound to your DGV, and then you bind the textbox to the same data source
with the same binding source. I think it will show the same information?
Have you tried that?

Are you using data tables/datasets or business objects? If you are using
business objects, you can implement INotifyPropertyChanged and it will
repaint the grid automatically.

Robin S.
---------------------
 
G

Guest

Robin,
Thanks for your reply.

RobinS said:
I guess my question would be why are you doing this? It seems to me to make
more sense for the user to put in all the fields and then maybe hit a
<Save> button and it upgrades all the entries in that row at the same time.
Maybe he would like to see what the old values are before committing the
new ones?

I want the user to see the results of their changes in the dgv in real time.
This is why I want this behavior. If I don't do this, the behavior can be
confusing. Besides textboxes, I also have checkboxes. If the default behavior
is retained, it isn't obvious to the user that they have successfully changed
the checkbox's value until the leave the control. The next control they need
to click might be the Save button, but there are some validation rules which
disable the save button. In this case, it seems clumsy to ask the user to tab
off every control to see his changes, especially if they only want to click
Save.
If you are committed to this course of action, my question is when you say
this:


what do you mean? Are you forcing an EndEdit() on the binding source, or a
Validate() or do you mean actually updating the data source?

When I say this is the default behavior, I mean that it's how the
BindingSource/dgv work without any intervention on my part. I already have
both the dgv and the individual textboxes bound to the same bindingSource.
When you do this, the dgv only reflects changes to the bndingSource, which is
only updated when the textbox's value is committed, by exiting the control.
Seems to me you might be able to accomplish this by databinding the same
data source to the same binding source. So you have a table that is data
bound to your DGV, and then you bind the textbox to the same data source
with the same binding source. I think it will show the same information?
Have you tried that?

Are you using data tables/datasets or business objects? If you are using
business objects, you can implement INotifyPropertyChanged and it will
repaint the grid automatically.
I am binding to custom business objects which do implement
INotifyPropertyChanged. However, as I've mentioned, while it does repaint the
grid automatically, it does so only after the property in the bindingsource
is updated, which occurs only after the textbox's value is committed, when
leaving the control.

Barry
 
G

Guest

Robin,
Thanks for your reply.

RobinS said:
I guess my question would be why are you doing this? It seems to me to make
more sense for the user to put in all the fields and then maybe hit a
<Save> button and it upgrades all the entries in that row at the same time.
Maybe he would like to see what the old values are before committing the
new ones?

I want the user to see the results of their changes in the dgv in real time.
This is why I want this behavior. If I don't do this, the behavior can be
confusing. Besides textboxes, I also have checkboxes. If the default behavior
is retained, it isn't obvious to the user that they have successfully changed
the checkbox's value until the leave the control. The next control they need
to click might be the Save button, but there are some validation rules which
disable the save button. In this case, it seems clumsy to ask the user to tab
off every control to see his changes, especially if they only want to click
Save.
If you are committed to this course of action, my question is when you say
this:


what do you mean? Are you forcing an EndEdit() on the binding source, or a
Validate() or do you mean actually updating the data source?

When I say this is the default behavior, I mean that it's how the
BindingSource/dgv work without any intervention on my part. I already have
both the dgv and the individual textboxes bound to the same bindingSource.
When you do this, the dgv only reflects changes to the bndingSource, which is
only updated when the textbox's value is committed, by exiting the control.
Seems to me you might be able to accomplish this by databinding the same
data source to the same binding source. So you have a table that is data
bound to your DGV, and then you bind the textbox to the same data source
with the same binding source. I think it will show the same information?
Have you tried that?

Are you using data tables/datasets or business objects? If you are using
business objects, you can implement INotifyPropertyChanged and it will
repaint the grid automatically.
I am binding to custom business objects which do implement
INotifyPropertyChanged. However, as I've mentioned, while it does repaint the
grid automatically, it does so only after the property in the bindingsource
is updated, which occurs only after the textbox's value is committed, when
leaving the control.

Barry
 
B

Brian Schwartz

Is there a reason you're using a read-only DataGridView instead of an
editable grid, which would save all this synchronization trouble?
 
G

Guest

Brian,

I beat my head against the wall for three weeks trying to use in-line
editing in the dgv. There were too many issues, especially with combobox
columns. In my humble opinion, the dgv is still a disappointing
work-in-process. Using seperate editing controls ended up being a better,
more intuitive solution.

Barry
 
G

Guest

For posterity, I've figured out how to do this:

myDataGridView.InvalidateRow(myDataGridView.CurrentRow.Index)

Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top