473,288 Members | 1,693 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,288 software developers and data experts.

Help: Need to cause DataGrid row to "flush" contents


I need some help programmatically causing a row in a DataGrid to "flush"
its contents to its bound data (in Visual Studio 6 using Windows Forms
with C#).

My issue is I want to send an update to a database from a menu command
while the user is editing a DataGrid. This is unusual in regard to
examples and normal practice in that the cell of the DataGrid still has
the focus. In all examples I can find, the user normally presses a
button on a Form. This practice causes the DataGrid to lose focus and
automatically flush the current row's contents to its bound data member.
But when I attempt to perform an update while the user is STILL editing
data in a row, the DataSet updates WITHOUT that particular row.

I attempted to circumvent this problem by simply setting the focus of
another control on the form (e.g. btnSave.Focus()), hoping that the
DataGrid would lose focus and flush its row contents to the DataSet, but
this proved to be inexplicably ineffective. The row was still not
flushed. I don't know if a Focus() call needs to iterate once through
the event chain before it finalizes or what.

I really want to do this the right way. I don't want to use a hack like
selecting another row before updating, but I can't find any method in
DataGrid that will force its contents to flush to its bound data. Does
anyone have the definitive procedure on this. Perhaps I'm simply missing
an obvious or well known method here?
Nov 15 '05 #1
11 5882
Junkguy,

I think that the best way to do this would be to call the EndEdit method
on the data grid before you perform the action. This will allow you to end
the current edit and should update the underlying data source when done.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com

"Junkguy" <ju*****@columbus.rr.com> wrote in message
news:ju***************************@news-server-fe-01.columbus.rr.com...

I need some help programmatically causing a row in a DataGrid to "flush"
its contents to its bound data (in Visual Studio 6 using Windows Forms
with C#).

My issue is I want to send an update to a database from a menu command
while the user is editing a DataGrid. This is unusual in regard to
examples and normal practice in that the cell of the DataGrid still has
the focus. In all examples I can find, the user normally presses a
button on a Form. This practice causes the DataGrid to lose focus and
automatically flush the current row's contents to its bound data member.
But when I attempt to perform an update while the user is STILL editing
data in a row, the DataSet updates WITHOUT that particular row.

I attempted to circumvent this problem by simply setting the focus of
another control on the form (e.g. btnSave.Focus()), hoping that the
DataGrid would lose focus and flush its row contents to the DataSet, but
this proved to be inexplicably ineffective. The row was still not
flushed. I don't know if a Focus() call needs to iterate once through
the event chain before it finalizes or what.

I really want to do this the right way. I don't want to use a hack like
selecting another row before updating, but I can't find any method in
DataGrid that will force its contents to flush to its bound data. Does
anyone have the definitive procedure on this. Perhaps I'm simply missing
an obvious or well known method here?

Nov 15 '05 #2
junkguy, what I do is the following:

this.grdWorksheet.CurrentCell = new DataGridCell(0,0);
this.grdWorksheet.CurrentCell = new DataGridCell(0,1);
this.grdWorksheet.CurrentCell = new DataGridCell(0,0);

this way, the focus changes to an existing cell in the datagrid. and
even if the new edited cell is in the same row, the changes will be
updated.

Erald Kulk
Nov 15 '05 #3
In article <Og**************@TK2MSFTNGP11.phx.gbl>,
"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com>
wrote:
I think that the best way to do this would be to call the EndEdit method
on the data grid before you perform the action. This will allow you to end
the current edit and should update the underlying data source when done.

Hope this helps.

Thanks Nicholas,

Wish it would help, but apparently EndEdit is already being called when
AcceptChanges() is called on the data table. I literally tried getting
the CurrencyManager and calling its EndCurrentEdit() call. It still
refused to work. As usual, there is some esoteric thing that happens to
really really make the thing end and nobody has caught the bug because
the whateveritiscall is getting coincidentally invoked by leaving the
DataGrid.

--Junkguy

--
Junkguy
Nov 15 '05 #4
In article <d9*************************@posting.google.com> ,
er***@delta-travel.nl (Erald Kulk) wrote:
this.grdWorksheet.CurrentCell = new DataGridCell(0,0);
this.grdWorksheet.CurrentCell = new DataGridCell(0,1);
this.grdWorksheet.CurrentCell = new DataGridCell(0,0);

this way, the focus changes to an existing cell in the datagrid. and
even if the new edited cell is in the same row, the changes will be
updated.


Yeah, that's cool.

I had tried

dg.CurrentCell = new DataGridCell(dg.CurrentRowIndex + 1,0);

This makes it go one row down, which I think is safer because if your
datagrid only contains one column, selecting the second cell won't work-
and in a datagrid that allows edits, there is always "one more row" on
the end. Or if you are on that end row, it does not affect it.

I tried earlier

dg.CurrentRowIndex += 1;

But that didn't work for some reason, and it throws an exception if you
are on the end row.

The only issue I have found with the "new cell" idea is when the row you
are currently on has a Validation error, the cell you were editing does
not re-highlight. It does re-hilight when you use the mouse to go to the
next row. Weird.

I really want to find out what the exact procedure is. I have actually
been stepping through the disassembled Microsoft code to see what the
heck they are doing. But that is pretty tedious and difficult and I
still haven't figured it out.

<rant>You know, EVERY other window toolkit I have ever used gives you
the code- which of course makes it easy to figure out what they are
doing. Microsoft hides it. I can see hiding the underlying operating
system but come on! The docs are so poor and they use so many esoteric
methodologies it just makes my job that much harder.</rant>

Until I figure out the correct procedures, I guess I'll use the "new
cell" method you describe.

Thanks for your advice.

--
Junkguy
Nov 15 '05 #5
Hi,
dg.CurrentCell = new DataGridCell(dg.CurrentRowIndex + 1,0);
I'd suggest

dg.CurrentCell = new DataGridCell(dg.CurrentCell.RowNumber + 1,0);

because CurrentRowIndex is hardwired to the parent table and this approach
won't work for a child table if you bind the grid to a master-detail
dataset.

Unfortunately I cannot access the message that started the thread, could you
please re-post your original problem and I could then probably suggest
something?

--
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

"Junkguy" <ju*****@columbus.rr.com> wrote in message
news:ju***************************@news-server-fe-01.columbus.rr.com... In article <d9*************************@posting.google.com> ,
er***@delta-travel.nl (Erald Kulk) wrote:
this.grdWorksheet.CurrentCell = new DataGridCell(0,0);
this.grdWorksheet.CurrentCell = new DataGridCell(0,1);
this.grdWorksheet.CurrentCell = new DataGridCell(0,0);

this way, the focus changes to an existing cell in the datagrid. and
even if the new edited cell is in the same row, the changes will be
updated.


Yeah, that's cool.

I had tried

dg.CurrentCell = new DataGridCell(dg.CurrentRowIndex + 1,0);

This makes it go one row down, which I think is safer because if your
datagrid only contains one column, selecting the second cell won't work-
and in a datagrid that allows edits, there is always "one more row" on
the end. Or if you are on that end row, it does not affect it.

I tried earlier

dg.CurrentRowIndex += 1;

But that didn't work for some reason, and it throws an exception if you
are on the end row.

The only issue I have found with the "new cell" idea is when the row you
are currently on has a Validation error, the cell you were editing does
not re-highlight. It does re-hilight when you use the mouse to go to the
next row. Weird.

I really want to find out what the exact procedure is. I have actually
been stepping through the disassembled Microsoft code to see what the
heck they are doing. But that is pretty tedious and difficult and I
still haven't figured it out.

<rant>You know, EVERY other window toolkit I have ever used gives you
the code- which of course makes it easy to figure out what they are
doing. Microsoft hides it. I can see hiding the underlying operating
system but come on! The docs are so poor and they use so many esoteric
methodologies it just makes my job that much harder.</rant>

Until I figure out the correct procedures, I guess I'll use the "new
cell" method you describe.

Thanks for your advice.

--
Junkguy


Nov 15 '05 #6
Hi,

The CurrencyManager.EndCurrentEdit followed by DataGrid.EndEdit sequence
works just fine for me. Note that it is a two-step sequence and any of the
steps alone won't do the job. If you follow this sequence and it doesn't
work, could you post a code snippet?

--
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

"Junkguy" <ju*****@columbus.rr.com> wrote in message
news:ju***************************@news-server-fe-01.columbus.rr.com...
In article <Og**************@TK2MSFTNGP11.phx.gbl>,
"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com>
wrote:
I think that the best way to do this would be to call the EndEdit method on the data grid before you perform the action. This will allow you to end the current edit and should update the underlying data source when done.

Hope this helps.

Thanks Nicholas,

Wish it would help, but apparently EndEdit is already being called when
AcceptChanges() is called on the data table. I literally tried getting
the CurrencyManager and calling its EndCurrentEdit() call. It still
refused to work. As usual, there is some esoteric thing that happens to
really really make the thing end and nobody has caught the bug because
the whateveritiscall is getting coincidentally invoked by leaving the
DataGrid.

--Junkguy

--
Junkguy


Nov 15 '05 #7
In article <eO**************@TK2MSFTNGP12.phx.gbl>,
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote:
The CurrencyManager.EndCurrentEdit followed by DataGrid.EndEdit sequence
works just fine for me.
Thanks,

The thing that concerned me about this was that somebody else might attempt to
call DataGrid.EndEdit() because it was automatically invoked (i.e. I didn't
call BeginEdit). DataGrid.EndEdit() needs a DataGridColumnStyle, row number,
and a boolean. Who the heck is holding this information while I'm editing a
cell? I would like to get them to do the work. Barring that, I'm a little
unsure as to the best way to get the current DataGridColumnStyle as DataGrid
does not seem to provide an easy means to it. How do you do get it securely
and then insure that nobody else will make the EndEdit call?

P.S. Here is the original posting I made as per your request:
I need some help programmatically causing a row in a DataGrid to "flush"
its contents to its bound data (in Visual Studio 6 using Windows Forms
with C#).

My issue is I want to send an update to a database from a menu command
while the user is editing a DataGrid. This is unusual in regard to
examples and normal practice in that the cell of the DataGrid still has
the focus. In all examples I can find, the user normally presses a
button on a Form. This practice causes the DataGrid to lose focus and
automatically flush the current row's contents to its bound data member.
But when I attempt to perform an update while the user is STILL editing
data in a row, the DataSet updates WITHOUT that particular row.

I attempted to circumvent this problem by simply setting the focus of
another control on the form (e.g. btnSave.Focus()), hoping that the
DataGrid would lose focus and flush its row contents to the DataSet, but
this proved to be inexplicably ineffective. The row was still not
flushed. I don't know if a Focus() call needs to iterate once through
the event chain before it finalizes or what.

I really want to do this the right way. I don't want to use a hack like
selecting another row before updating, but I can't find any method in
DataGrid that will force its contents to flush to its bound data. Does
anyone have the definitive procedure on this. Perhaps I'm simply missing
an obvious or well known method here?


--
Junkguy
Nov 15 '05 #8
> The thing that concerned me about this was that somebody else might
attempt to
call DataGrid.EndEdit() because it was automatically invoked (i.e. I didn't call BeginEdit).
No problem with that. To the best of my knowledge, no exception will be
thrown and no bad thing will happen. The method will just return false.
DataGrid.EndEdit() needs a DataGridColumnStyle, row number, and a boolean.
Who the heck is holding this information while I'm editing a cell?
The row number can be obtained from dataGrid.CurrentCell.RowNumber.
The boolean just means whether you want to commit changes or roll them back.
The column style is the hardest thing. The only way I know is having column
styles created manually through the TableStyles/GridColumnStyles collection
designers and then obtaining the corresponding instance by the column
number. The column number is also determined frin the CurrentCell property.

P.S. I am currently finishing work on a DataGrid article that should cover
this issue in particular.

--
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

"Junkguy" <ju*****@columbus.rr.com> wrote in message
news:ju***************************@news-server-fe-02.columbus.rr.com... In article <eO**************@TK2MSFTNGP12.phx.gbl>,
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote:
The CurrencyManager.EndCurrentEdit followed by DataGrid.EndEdit sequence
works just fine for me.
Thanks,

The thing that concerned me about this was that somebody else might

attempt to call DataGrid.EndEdit() because it was automatically invoked (i.e. I didn't call BeginEdit). DataGrid.EndEdit() needs a DataGridColumnStyle, row number, and a boolean. Who the heck is holding this information while I'm editing a cell? I would like to get them to do the work. Barring that, I'm a little
unsure as to the best way to get the current DataGridColumnStyle as DataGrid does not seem to provide an easy means to it. How do you do get it securely and then insure that nobody else will make the EndEdit call?

P.S. Here is the original posting I made as per your request:
I need some help programmatically causing a row in a DataGrid to "flush"
its contents to its bound data (in Visual Studio 6 using Windows Forms
with C#).

My issue is I want to send an update to a database from a menu command
while the user is editing a DataGrid. This is unusual in regard to
examples and normal practice in that the cell of the DataGrid still has
the focus. In all examples I can find, the user normally presses a
button on a Form. This practice causes the DataGrid to lose focus and
automatically flush the current row's contents to its bound data member.
But when I attempt to perform an update while the user is STILL editing
data in a row, the DataSet updates WITHOUT that particular row.

I attempted to circumvent this problem by simply setting the focus of
another control on the form (e.g. btnSave.Focus()), hoping that the
DataGrid would lose focus and flush its row contents to the DataSet, but
this proved to be inexplicably ineffective. The row was still not
flushed. I don't know if a Focus() call needs to iterate once through
the event chain before it finalizes or what.

I really want to do this the right way. I don't want to use a hack like
selecting another row before updating, but I can't find any method in
DataGrid that will force its contents to flush to its bound data. Does
anyone have the definitive procedure on this. Perhaps I'm simply missing
an obvious or well known method here?


--
Junkguy


Nov 15 '05 #9
In article <u1**************@TK2MSFTNGP12.phx.gbl>,
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote:
DataGrid.EndEdit() needs a DataGridColumnStyle, row number, and a boolean.
Who the heck is holding this information while I'm editing a cell?


Thanks Dmitriy for you response.

We still do not know what called BeginEdit. Some object, somewhere made the
initial call and still holds the info. I think I might just step through the
assembly code and find out.
The row number can be obtained from dataGrid.CurrentCell.RowNumber.
The boolean just means whether you want to commit changes or roll them back.
The column style is the hardest thing. The only way I know is having column
styles created manually through the TableStyles/GridColumnStyles collection
designers and then obtaining the corresponding instance by the column
number.


Well there's the rub isn't it. Getting the current column style is the most
mind-numbingly difficult thing and it should not be. Committing a row to the
data member should not be like pulling teeth. It should be a common
programmer-accessible call without having to go through hoops.
Anyway, thanks again. I look forward to reading your article.

--
Junkguy
Nov 15 '05 #10
Junkguy,
We still do not know what called BeginEdit. Some object, somewhere made the initial call and still holds the info. I think I might just step through the assembly code and find out.
I wonder why would you need to find out this information? In my exerience, I
was always happily calling EndEdit without caring who had called BeginEdit.
When you know the row and the column, that's all you need to force the grid
commit editing.
Well there's the rub isn't it. Getting the current column style is the most mind-numbingly difficult thing and it should not be. Committing a row to the data member should not be like pulling teeth. It should be a common
programmer-accessible call without having to go through hoops.
Wholeheartedly agree with this. The DataGrid is full of secrets and
surprises, like a good computer game should be :-))

--
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

"Junkguy" <ju*****@columbus.rr.com> wrote in message
news:ju***************************@news-server-fe-01.columbus.rr.com... In article <u1**************@TK2MSFTNGP12.phx.gbl>,
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote:
DataGrid.EndEdit() needs a DataGridColumnStyle, row number, and a boolean. Who the heck is holding this information while I'm editing a cell?


Thanks Dmitriy for you response.

We still do not know what called BeginEdit. Some object, somewhere made

the initial call and still holds the info. I think I might just step through the assembly code and find out.
The row number can be obtained from dataGrid.CurrentCell.RowNumber.
The boolean just means whether you want to commit changes or roll them back. The column style is the hardest thing. The only way I know is having column styles created manually through the TableStyles/GridColumnStyles collection designers and then obtaining the corresponding instance by the column
number.
Well there's the rub isn't it. Getting the current column style is the

most mind-numbingly difficult thing and it should not be. Committing a row to the data member should not be like pulling teeth. It should be a common
programmer-accessible call without having to go through hoops.
Anyway, thanks again. I look forward to reading your article.

--
Junkguy


Nov 15 '05 #11
In article <Oa**************@tk2msftngp13.phx.gbl>,
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote:
I wonder why would you need to find out this information? In my exerience, I
was always happily calling EndEdit without caring who had called BeginEdit.
When you know the row and the column, that's all you need to force the grid
commit editing.


Just because it would perform all the nessesary steps to clean up after
itself. It may well be that nobody holds the info, and when you leave a cell
(for example) some generic code just finds the current column style and calls
EndEdit itself. If EndEdit is truly all that needs called then your suggestion
is fine.

I was simply concerned that perhaps some call X other than EndEdit was being
called first (i.e. EndEdit is called inside X) and X would have some cleanup
code inside it that should be done (like maybe textbox validation or a
delegate call). If we call EndEdit ourselves and skip this cleanup in call X,
we may well skip some important code, or worse, it may break sometime in the
future when Microsoft ammends call X.

But I'm probably just being paranoid. Thanks for your help.

--
Junkguy
Nov 15 '05 #12

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

Similar topics

8
by: Jerry | last post by:
Hi All How can I produce an output to the browser and let the script continue to work in the background, producing a later screen output? Example: Trigger a database search so that the user...
4
by: Eric West | last post by:
Greetings- I have a web application that has a form that triggers a server process that I would like to provide a "Searching..." page for. My strategy was to send the browser the "top half" of...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
3
by: Jimski | last post by:
Hello all, I am having a problem where I get an error message when I call FlushFinalBlock when decrypting my encrypted text. I am using the Rijndael algorithm. The error message is "Length...
7
by: theyas | last post by:
How can I get my code to NOT display two "Open/Save/Cancel/More Info" dialog boxes when using the "Response.WriteFile" method to download a file to IE I've asked about this before and didn't get a...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
3
by: Evan | last post by:
So I have two versions of a program (these are complete programs, not excerpts): Version 1: template <class T > void foo() { return bar( T() ); }
5
by: arnuld | last post by:
this is from mentioned section. i did not understand some things here: it means "flushing the buffer" and "writing to output device" are SAME thing, these are just 2 different names for the...
1
by: Sharon | last post by:
Hi, I have a smarty template with about 500 rows, that take a long time to load. I was wondering if there was a way to display it by parts (i.e. first of all display the first 100 rows, then rows...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.