473,398 Members | 2,343 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,398 software developers and data experts.

How do convince a DataGridView it's been edited?

The situation:

* I have a ButtonColumn in a DataGridView.
* When the user preses one of the buttons, a dialog appears.
* Based on what the user selects in the dialog, data is entered
programmatically into the the underlying cell (i.e., I'm setting Value
property of the cell based on user input, but the user is not directly
entering any data).

The problem:

The DataGridView doesn't seem believe the user has edited the row, so
if the user clicks or tabs somewhere else (which causes the
DataGridView to lose focus), all the changes to the row get
cleared/canceled.

Other information:

Looking at the RowHeaderCell for the row as it's being edited, it
changes from a large asterisk[*] to a right arrow + asterisk [>*].
This is different than if the user directly edits a cell by typing (in
that case, you'd get a pencil icon while the cell is in edit mode
followed by a large right arrow after the change is committed).

My question:

So, how do I programmatically tell the DataGridView that the user
really has touched/edited this row and that the data should stick even
if the DataGridView loses focus.

Thanks!

-Dan

Jan 24 '07 #1
5 3773
If the grid is bound, edit the underlying data source. I dont ever add rows
and columns to a grid with out binding it. I make a DataTable first and let
that be the container for the data. Then I can bind the grid to it and update
the DataTable when needs be.

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"DanThMan" wrote:
The situation:

* I have a ButtonColumn in a DataGridView.
* When the user preses one of the buttons, a dialog appears.
* Based on what the user selects in the dialog, data is entered
programmatically into the the underlying cell (i.e., I'm setting Value
property of the cell based on user input, but the user is not directly
entering any data).

The problem:

The DataGridView doesn't seem believe the user has edited the row, so
if the user clicks or tabs somewhere else (which causes the
DataGridView to lose focus), all the changes to the row get
cleared/canceled.

Other information:

Looking at the RowHeaderCell for the row as it's being edited, it
changes from a large asterisk[*] to a right arrow + asterisk [>*].
This is different than if the user directly edits a cell by typing (in
that case, you'd get a pencil icon while the cell is in edit mode
followed by a large right arrow after the change is committed).

My question:

So, how do I programmatically tell the DataGridView that the user
really has touched/edited this row and that the data should stick even
if the DataGridView loses focus.

Thanks!

-Dan

Jan 25 '07 #2
Thanks to both of you for your replies.

The problem is that I'm not creating a new row programmatically then
filling it with values. Rather, the user is interacting with the "row
for new records" and I'm programmatically entering values into that row
based on what the user does.

If I were just trying to add a new record entirely through code, I
could easily see how this would be done using the underlying DataTable.
But, in this case, if I were to add a new row to the DataTable when the
user interacts with row for new records, It seems like I'd just end up
with two unsync'd versions of the same row.

I like the SendKeys idea, but it only works for tables where there is a
visible DataGridViewTextBoxCell available to send input to. Sending
keys to a DataGridViewButtonCell, for example, doesn't trick the
DataGridView into thinking the row has been edited.

Is there really no way to simply set the status of a row to
edited/changed?

There seems to be a way to set the DirtyState of a cell to True
(NotifyCurrentCellDirty), but this still isn't enough to prevent the
problem of all values disappearing when the row loses focus. I also
can't seem to programmatically change the CurrentCell to any other type
of cell besides a DataGridViewTextBoxCell.

Right now, the only solution I can think of is to create a dummy
DataGridViewTextBoxColumn in a every table just so that I can use
SendKeys with it. Someone please tell me I don't have to resort to
something that kludgey!

Thanks again,

-Dan

On Jan 25, 2:41 am, "ClayB" <c...@syncfusion.comwrote:
One other comment in addition to what Ciaran suggested is that the row
with the * next to it is the AddNew row so it really does not exist in
your DataSource. So, if you want to use Ciaran's suggestion of
interacting directly with the DataSource, then you would have to
actually add a new item (DataRow) in some manner, maybe calling
DataTable.NewRow, change the values in this new row, and then call
DataTable.Rows.Add to add the new row into the DataTable.

If you are only changing a single value in this new row, and want to do
it through the UI (and not directly work with the DataTable), then you
can try using SendKeys to simiulate keystrokes into the grid to set a
value, but there may be consequences of doing it this way. Manipulating
the data in the DataSource is probably the most robust way to do this.

//set the current cell and set it editing
this.dataGridView1.CurrentCell = this.dataGridView1[2, 10];
this.dataGridView1.BeginEdit(true);
//send the keystrokes and end the editing
SendKeys.Send(SomeValue.ToString());
this.dataGridView1.EndEdit();

=========================
Clay Burch
Syncfusion, Inc.
Jan 25 '07 #3
Why can't you create a new row for your datatable, and put the user's
entries into it, then add it to the DataGridView?

What are you binding your datagridview to? Is it by chance a list of
objects? Or is it a datatable or dataset?

Robin S.
---------------------------------
"DanThMan" <da******@cox.netwrote in message
news:11*********************@v45g2000cwv.googlegro ups.com...
Thanks to both of you for your replies.

The problem is that I'm not creating a new row programmatically then
filling it with values. Rather, the user is interacting with the "row
for new records" and I'm programmatically entering values into that
row
based on what the user does.

If I were just trying to add a new record entirely through code, I
could easily see how this would be done using the underlying
DataTable.
But, in this case, if I were to add a new row to the DataTable when
the
user interacts with row for new records, It seems like I'd just end up
with two unsync'd versions of the same row.

I like the SendKeys idea, but it only works for tables where there is
a
visible DataGridViewTextBoxCell available to send input to. Sending
keys to a DataGridViewButtonCell, for example, doesn't trick the
DataGridView into thinking the row has been edited.

Is there really no way to simply set the status of a row to
edited/changed?

There seems to be a way to set the DirtyState of a cell to True
(NotifyCurrentCellDirty), but this still isn't enough to prevent the
problem of all values disappearing when the row loses focus. I also
can't seem to programmatically change the CurrentCell to any other
type
of cell besides a DataGridViewTextBoxCell.

Right now, the only solution I can think of is to create a dummy
DataGridViewTextBoxColumn in a every table just so that I can use
SendKeys with it. Someone please tell me I don't have to resort to
something that kludgey!

Thanks again,

-Dan

On Jan 25, 2:41 am, "ClayB" <c...@syncfusion.comwrote:
>One other comment in addition to what Ciaran suggested is that the
row
with the * next to it is the AddNew row so it really does not exist
in
your DataSource. So, if you want to use Ciaran's suggestion of
interacting directly with the DataSource, then you would have to
actually add a new item (DataRow) in some manner, maybe calling
DataTable.NewRow, change the values in this new row, and then call
DataTable.Rows.Add to add the new row into the DataTable.

If you are only changing a single value in this new row, and want to
do
it through the UI (and not directly work with the DataTable), then
you
can try using SendKeys to simiulate keystrokes into the grid to set a
value, but there may be consequences of doing it this way.
Manipulating
the data in the DataSource is probably the most robust way to do
this.

//set the current cell and set it editing
this.dataGridView1.CurrentCell = this.dataGridView1[2,
10];
this.dataGridView1.BeginEdit(true);
//send the keystrokes and end the editing
SendKeys.Send(SomeValue.ToString());
this.dataGridView1.EndEdit();

=========================
Clay Burch
Syncfusion, Inc.

Jan 25 '07 #4
It looks like the solution was to place the following code in the
DataGridView'sRowLeave event handler:

'make sure row is not empty
'binding = reference to the binding for the DataGridView
binding.SuspendBinding()
binding.ResumeBinding()

I'd feel better about this if I knew why it worked. I just sort of
stumbled upon it after playing with several different solutions,
including using SendKeys and adding a row to the DataTable.

Thanks for all the advice,

-Dan

On Jan 25, 2:09 pm, "RobinS" <Rob...@NoSpam.yah.nonewrote:
Why can't you create a new row for your datatable, and put the user's
entries into it, then add it to the DataGridView?

What are you binding your datagridview to? Is it by chance a list of
objects? Or is it a datatable or dataset?

Robin S.
---------------------------------"DanThMan" <danth...@cox.netwrote in messagenews:11*********************@v45g2000cwv.go oglegroups.com...
Thanks to both of you for your replies.
The problem is that I'm not creating a new row programmatically then
filling it with values. Rather, the user is interacting with the "row
for new records" and I'm programmatically entering values into that
row
based on what the user does.
If I were just trying to add a new record entirely through code, I
could easily see how this would be done using the underlying
DataTable.
But, in this case, if I were to add a new row to the DataTable when
the
user interacts with row for new records, It seems like I'd just end up
with two unsync'd versions of the same row.
I like the SendKeys idea, but it only works for tables where there is
a
visible DataGridViewTextBoxCell available to send input to. Sending
keys to a DataGridViewButtonCell, for example, doesn't trick the
DataGridView into thinking the row has been edited.
Is there really no way to simply set the status of a row to
edited/changed?
There seems to be a way to set the DirtyState of a cell to True
(NotifyCurrentCellDirty), but this still isn't enough to prevent the
problem of all values disappearing when the row loses focus. I also
can't seem to programmatically change the CurrentCell to any other
type
of cell besides a DataGridViewTextBoxCell.
Right now, the only solution I can think of is to create a dummy
DataGridViewTextBoxColumn in a every table just so that I can use
SendKeys with it. Someone please tell me I don't have to resort to
something that kludgey!
Thanks again,
-Dan
On Jan 25, 2:41 am, "ClayB" <c...@syncfusion.comwrote:
One other comment in addition to what Ciaran suggested is that the
row
with the * next to it is the AddNew row so it really does not exist
in
your DataSource. So, if you want to use Ciaran's suggestion of
interacting directly with the DataSource, then you would have to
actually add a new item (DataRow) in some manner, maybe calling
DataTable.NewRow, change the values in this new row, and then call
DataTable.Rows.Add to add the new row into the DataTable.
If you are only changing a single value in this new row, and want to
do
it through the UI (and not directly work with the DataTable), then
you
can try using SendKeys to simiulate keystrokes into the grid to set a
value, but there may be consequences of doing it this way.
Manipulating
the data in the DataSource is probably the most robust way to do
this.
//set the current cell and set it editing
this.dataGridView1.CurrentCell = this.dataGridView1[2,
10];
this.dataGridView1.BeginEdit(true);
//send the keystrokes and end the editing
SendKeys.Send(SomeValue.ToString());
this.dataGridView1.EndEdit();
=========================
Clay Burch
Syncfusion, Inc.
Jan 26 '07 #5
I have found that if I want to be sure that changes to a cell in a
datagridview are made that I need to click on any other row before doing the
update command for the dataadapter.

I am sure there is another way.
"DanThMan" <da******@cox.netwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
It looks like the solution was to place the following code in the
DataGridView'sRowLeave event handler:

'make sure row is not empty
'binding = reference to the binding for the DataGridView
binding.SuspendBinding()
binding.ResumeBinding()

I'd feel better about this if I knew why it worked. I just sort of
stumbled upon it after playing with several different solutions,
including using SendKeys and adding a row to the DataTable.

Thanks for all the advice,

-Dan

On Jan 25, 2:09 pm, "RobinS" <Rob...@NoSpam.yah.nonewrote:
>Why can't you create a new row for your datatable, and put the user's
entries into it, then add it to the DataGridView?

What are you binding your datagridview to? Is it by chance a list of
objects? Or is it a datatable or dataset?

Robin S.
---------------------------------"DanThMan" <danth...@cox.netwrote in
messagenews:11*********************@v45g2000cwv.g ooglegroups.com...
Thanks to both of you for your replies.
The problem is that I'm not creating a new row programmatically then
filling it with values. Rather, the user is interacting with the "row
for new records" and I'm programmatically entering values into that
row
based on what the user does.
If I were just trying to add a new record entirely through code, I
could easily see how this would be done using the underlying
DataTable.
But, in this case, if I were to add a new row to the DataTable when
the
user interacts with row for new records, It seems like I'd just end up
with two unsync'd versions of the same row.
I like the SendKeys idea, but it only works for tables where there is
a
visible DataGridViewTextBoxCell available to send input to. Sending
keys to a DataGridViewButtonCell, for example, doesn't trick the
DataGridView into thinking the row has been edited.
Is there really no way to simply set the status of a row to
edited/changed?
There seems to be a way to set the DirtyState of a cell to True
(NotifyCurrentCellDirty), but this still isn't enough to prevent the
problem of all values disappearing when the row loses focus. I also
can't seem to programmatically change the CurrentCell to any other
type
of cell besides a DataGridViewTextBoxCell.
Right now, the only solution I can think of is to create a dummy
DataGridViewTextBoxColumn in a every table just so that I can use
SendKeys with it. Someone please tell me I don't have to resort to
something that kludgey!
Thanks again,
-Dan
On Jan 25, 2:41 am, "ClayB" <c...@syncfusion.comwrote:
One other comment in addition to what Ciaran suggested is that the
row
with the * next to it is the AddNew row so it really does not exist
in
your DataSource. So, if you want to use Ciaran's suggestion of
interacting directly with the DataSource, then you would have to
actually add a new item (DataRow) in some manner, maybe calling
DataTable.NewRow, change the values in this new row, and then call
DataTable.Rows.Add to add the new row into the DataTable.
>If you are only changing a single value in this new row, and want to
do
it through the UI (and not directly work with the DataTable), then
you
can try using SendKeys to simiulate keystrokes into the grid to set a
value, but there may be consequences of doing it this way.
Manipulating
the data in the DataSource is probably the most robust way to do
this.
> //set the current cell and set it editing
this.dataGridView1.CurrentCell = this.dataGridView1[2,
10];
this.dataGridView1.BeginEdit(true);
//send the keystrokes and end the editing
SendKeys.Send(SomeValue.ToString());
this.dataGridView1.EndEdit();
>=========================
Clay Burch
Syncfusion, Inc.

Jan 26 '07 #6

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

Similar topics

1
by: Arvind | last post by:
Hi I edit a cell in a DataGridView, and then would move to another cell in the DataGridView using mouse. Then the DataGridView as of now does scroll down to the last visited cell. But when I try...
0
by: RSH | last post by:
I am working with the DataGridView control in a WinForm. I have written the code to change the background color of a cell after it is edited. I also change the color of the row (a different...
2
by: bob | last post by:
Can anyone tell me the best way to update a dataset while it is being edited/viewed in the DataGridView control? Is this something that should be inserted into one of the grid's events? or should...
0
by: MKBender | last post by:
Hello everyone, What I'm currently trying to do is allow a user to add a row to my datagridview. I have AllowUserToAddRows set to true. And the empty now does appear at the bottom of my data. ...
5
by: DanThMan | last post by:
The situation: * I have a ButtonColumn in a DataGridView. * When the user preses one of the buttons, a dialog appears. * Based on what the user selects in the dialog, data is entered...
4
by: Terry Olsen | last post by:
How can i tell if a row in my DataGridView has been edited? I'm loading a DataTable with ID3 information from MP3 files and using that as the datasource for the DataGridView. I'm letting the user...
1
by: kristian | last post by:
Hi all I have a simple form with not much more than a datagridview control, and want to show and edit the contents of a table in my mysql table. So far I have managed to set the db table as a...
0
by: RickH | last post by:
Has anyone tried wrapping the DataGridView control itself as a custom Cell/Column control. So that I can have a child DataGridView appear in a column of a parent DataGridView so that the...
12
by: cj | last post by:
When viewing a datatable in a datagridview one of the columns in it is a "note" field which can be quite long. I would like to have the note field of the currently selected row of the datagrid...
3
LegalIT
by: LegalIT | last post by:
Hello, I am using VB.net. I have a DataGridView that is loaded with a transaction history from a database by a call to a subroutine called LoadHistory(). I have three buttons that open new...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.