By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,154 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,154 IT Pros & Developers. It's quick & easy.

Datagrid and currentcellchanged event

P: n/a
Hi all,

I'm having problems with datagrids and the currentcellchanged event.
My problem is this: I have a datagrid on a form, if the user changes
the text in a cell on the datagrid then tries to close the form via a
button on the toolbar (with going to another cell in the datagrid)I
want to be able to popup a messagebox to the user asking them if they
want to keep their changes.

At the momment I'm accomplishing this by a property that I set to true
in the currentcellchanged event. However if the user just tabs around
the datagrid this event is triggered even though though from the
user's point of view they haven't changed any text.

I still want the user to be able to tab around the datagrid but
perhaps not have the tab key trigger the currentcellchanged event. Is
this possible?

Another way I have thought of doing this is by trying to get the
current datagrid cell's value (using datagrid.text) and then comparing
it to the underlaying datasource's value for that cell (& if the 2 are
different then setting my property to true). However I can't find a
way of getting hold of the datagrid's datasource's value for the
current datagrid cell.

Any ideas would be most gratefully received - one more thing - my
datagrid is a custom control and I would like the above functionality
to be included within the custom control rather than the form that it
sits on.

Thanks in advance for any help
Suzanne
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Suzanne,
I still want the user to be able to tab around the datagrid but
perhaps not have the tab key trigger the currentcellchanged event. Is
this possible?
You cannot do that, well, unless you don't inherit from DataGrid and tap
into its event handling logic. And I'd say this is not the best way to do
so.
Another way I have thought of doing this is by trying to get the
current datagrid cell's value (using datagrid.text) and then comparing
it to the underlaying datasource's value for that cell (& if the 2 are
different then setting my property to true). However I can't find a
way of getting hold of the datagrid's datasource's value for the
current datagrid cell.
I like this approach much better. And it is relatively simple to get hold of
the corresponding source data row:

CurrencyManager cm = this.BindingContext[dataGrid.DataSource,
dataGrid1.DataMember];
DataView view = (DataView)cm.List;
DataRow theRow = view[dataGrid1.CurrentCell.RowNumber /* -1 probably
*/].Row;

You can then analyze theRow's RowState property to see if it has been
changed.

P.S. I beleive this code can easily be modified to become a part of a custom
DataGrid-based control.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Suzanne" <Su***********@mhapensions.com> wrote in message
news:87*************************@posting.google.co m... Hi all,

I'm having problems with datagrids and the currentcellchanged event.
My problem is this: I have a datagrid on a form, if the user changes
the text in a cell on the datagrid then tries to close the form via a
button on the toolbar (with going to another cell in the datagrid)I
want to be able to popup a messagebox to the user asking them if they
want to keep their changes.

At the momment I'm accomplishing this by a property that I set to true
in the currentcellchanged event. However if the user just tabs around
the datagrid this event is triggered even though though from the
user's point of view they haven't changed any text.

I still want the user to be able to tab around the datagrid but
perhaps not have the tab key trigger the currentcellchanged event. Is
this possible?

Another way I have thought of doing this is by trying to get the
current datagrid cell's value (using datagrid.text) and then comparing
it to the underlaying datasource's value for that cell (& if the 2 are
different then setting my property to true). However I can't find a
way of getting hold of the datagrid's datasource's value for the
current datagrid cell.

Any ideas would be most gratefully received - one more thing - my
datagrid is a custom control and I would like the above functionality
to be included within the custom control rather than the form that it
sits on.

Thanks in advance for any help
Suzanne


Nov 20 '05 #2

P: n/a
Can't you use the datarowversion.original from each cell?
Or do I misunderstand the question?
oldvalue= tmprow("fieldname", DataRowVersion.Original)

newvalue=tmprow("fieldname",datarowversion.modifie d)

tmprow is a row in the table where you putted the changes in

Dim updatedrows As DataTable =
dataset1.Tables(0).GetChanges(DataRowState.Modifie d)

Tupolev

"Suzanne" <Su***********@mhapensions.com> schreef in bericht
news:87*************************@posting.google.co m...
Hi all,

I'm having problems with datagrids and the currentcellchanged event.
My problem is this: I have a datagrid on a form, if the user changes
the text in a cell on the datagrid then tries to close the form via a
button on the toolbar (with going to another cell in the datagrid)I
want to be able to popup a messagebox to the user asking them if they
want to keep their changes.

At the momment I'm accomplishing this by a property that I set to true
in the currentcellchanged event. However if the user just tabs around
the datagrid this event is triggered even though though from the
user's point of view they haven't changed any text.

I still want the user to be able to tab around the datagrid but
perhaps not have the tab key trigger the currentcellchanged event. Is
this possible?

Another way I have thought of doing this is by trying to get the
current datagrid cell's value (using datagrid.text) and then comparing
it to the underlaying datasource's value for that cell (& if the 2 are
different then setting my property to true). However I can't find a
way of getting hold of the datagrid's datasource's value for the
current datagrid cell.

Any ideas would be most gratefully received - one more thing - my
datagrid is a custom control and I would like the above functionality
to be included within the custom control rather than the form that it
sits on.

Thanks in advance for any help
Suzanne

Nov 20 '05 #3

P: n/a
Thanks for all the suggestions, unfortunately it's still not behaving
exactly as I'd planned...

I'm unable to use the versioning mechanism Mr. Tupolev suggests as
when the user clicks on the toolbar (with the focus still being in the
edited cell of the datagrid) I don't think that the underlying
datatable has yet been updated with any changes? Which is really the
whole problem!

Dmitriy's suggestions for using the currency manager to get the
corresponding source data row were alot closer to what I was looking
for (and does return this value) - however I was planning to check the
value of underlaying datasource's value for that cell against the new
text of the datagrid cell when the currentcellchanged event was
triggered.

The problem is that this event is trigged as soon as the user tabs
into the cell and then I guess for every keystroke afterwards? So I
don't know when my user has finshed editing the cell (if it is the
custom control datagrid that is handling this functionality rather
than the form it sits on).

The solution that I'm using - which works - but is not pretty - is
that in the code for my form, when the user clicks on the toolbar I
run a bit of code that sets the currentcell of my datagrid to the 1st
cell in the grid (if the user is not currently editing the first cell,
if they are then I set the current cell to be another cell). Basically
I just force the datagrid to update its datasource by moving out of
the edited cell, so I don't need to use the currentcellchanged event
at all.

If any one can think of a more elegant way to do this then I'd be most
interested

Again thanks for all the help

Suzanne
Nov 20 '05 #4

P: n/a
Hi Susanne,

Have you tried using the aftercoledit event. It might do what you want. If
nothing has been changed, the event won't fire.

Suzette

"Suzanne" <Su***********@mhapensions.com> wrote in message
news:87*************************@posting.google.co m...
Hi all,

I'm having problems with datagrids and the currentcellchanged event.
My problem is this: I have a datagrid on a form, if the user changes
the text in a cell on the datagrid then tries to close the form via a
button on the toolbar (with going to another cell in the datagrid)I
want to be able to popup a messagebox to the user asking them if they
want to keep their changes.

At the momment I'm accomplishing this by a property that I set to true
in the currentcellchanged event. However if the user just tabs around
the datagrid this event is triggered even though though from the
user's point of view they haven't changed any text.

I still want the user to be able to tab around the datagrid but
perhaps not have the tab key trigger the currentcellchanged event. Is
this possible?

Another way I have thought of doing this is by trying to get the
current datagrid cell's value (using datagrid.text) and then comparing
it to the underlaying datasource's value for that cell (& if the 2 are
different then setting my property to true). However I can't find a
way of getting hold of the datagrid's datasource's value for the
current datagrid cell.

Any ideas would be most gratefully received - one more thing - my
datagrid is a custom control and I would like the above functionality
to be included within the custom control rather than the form that it
sits on.

Thanks in advance for any help
Suzanne

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.