Detecting Changes in Datagrid?

M

Moogy

How can I detect when changes are been made in the Datagrid? I don't see
any event for this, and the control obviously knows when changes have been
made...

I currently have it hooked into a Dataset.

Thanks!

Moogy
 
M

Miha Markic [MVP C#]

Hi Moogy,

Moogy said:
How can I detect when changes are been made in the Datagrid? I don't see
any event for this, and the control obviously knows when changes have been
made...

I currently have it hooked into a Dataset.

What's wrong with that?
 
D

dm_dal

There are a couple of ways to do this that I am aware of (maybe someone else
has a better solution).

Detecting changes is done in the dataset via the:
RowChanged and the ColumnChanged events;

Example: MyDataSet has one table called MyTable

I write a custom event handler for the MyDataSet.MyTable.RowChanged event;
MyDataSet.MyTable.RowChange += new
DataRowChangeEventHandler(MyTable_RowChanged);

This will fire when a change is made to a cell and the "Row" looses focus
(ie navigate to another row);

If I write a event handler for the ColumnChange event:
MyDataSet.MyTable.ColumnChange += new
DataColumnChangeEventHandler(MyTable_ColumnChanged);

This will fire after a change is made and the cell looses focus (ie navigate
to another cell)

Hope this helps,
David
 
M

Moogy

That's what I'm currently doing; seems rather rediculous though to have to
do this rather than receive an event that the actual data has changed.
Also, with the below method (which may be the best unfortunately) I need to
keep track of values in the cells...

I was hoping there was something obvious that I missed here; but if not,
whoever wrote the DataGrid should be zapped with a cattleprod...

Moogy!
 
M

Moogy

What's wrong is I need to know when they've actually "changed" the data so I
can enable my COMMIT button.

Moogy!
 
M

Miha Markic [MVP C#]

Hi Moogy,

I think ColumnChanged/Changing does exactly that.
Or you want to detect changes even before user leaves the cell?
 
M

Moogy

Correct; I need to detect that they've changed the actual data, not moved
between cells.

Thanks!

MOOGY!
 
M

Miha Markic [MVP C#]

Moogy,

We shall clear one thing though:
When user edits data within cell only grid knows it. When user moves out of
the cell the grid commits the changes to datasource.
If you want to detect the changes before they are commited to datasource
imagine this scenarion:
User starts modifying value in a cell (and you detect it and enable commit
button).
But then, user might change his/her mind and will press ESC so the changes
will be lost.
I don't think that you'll be able to implement this logic with DataGrid.
 
M

Moogy

This makes no sense; the datagrid has to know if the actual data has
changed; this is apparent in its behavior for type checking, etc.

When the cell is changed, it's obvious that an event is being fired in the
grid (internally) flagging a change in data. All I want is a piece of that
event...

MOOGY!


Miha Markic said:
Moogy,

We shall clear one thing though:
When user edits data within cell only grid knows it. When user moves out of
the cell the grid commits the changes to datasource.
If you want to detect the changes before they are commited to datasource
imagine this scenarion:
User starts modifying value in a cell (and you detect it and enable commit
button).
But then, user might change his/her mind and will press ESC so the changes
will be lost.
I don't think that you'll be able to implement this logic with DataGrid.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Moogy said:
Correct; I need to detect that they've changed the actual data, not moved
between cells.
 
M

Miha Markic [MVP C#]

Hi Moogy,

Moogy said:
This makes no sense; the datagrid has to know if the actual data has
changed; this is apparent in its behavior for type checking, etc.

When the cell is changed, it's obvious that an event is being fired in the
grid (internally) flagging a change in data.

Is it obvious?

All I want is a piece of that

AFAIK there isn't any at least not publicly exposed.
 
J

Jan

Moogy,

I suggest you follow Miha's advice of using Row/ColumnChanging/ed. However
if you really insist, and provided you only use textfields and no checkboxes
in the datagrid, there is always the TextBox property of the
DataGridTextBoxColumn object, which returns you the textbox in use inside
the datagrid for the field you are editing. This textbox has the usual
events like TextChanged, KeyPress, KeyUp, KeyDown....

Regards,
Jan
 
M

Moogy

This might work; how do I return the DataGridTextBox of the selected cell?
Technically I guess I just need to get the object from the style of the
selected column; is this what you are recommending?

MOOGY!
 
J

Jan

Moogy,

Hope this helps :

Dim IndexOfSelectedColumn = MyDatagrid.Currentcell.ColumnNumber
Dim MyTextBox as textbox =CType(
MyDatagrid.TableStyles(<MyBoundTableName>).GridColumnStyles(IndexOfSelectedC
olumn), DataGridTextBoxColumn ).textbox

Regards,
Jan
 
M

Moogy

I build the column styles myself, so I could hook in at that point.

Thanks for the information!

MOOGY!

Jan said:
Moogy,

Hope this helps :

Dim IndexOfSelectedColumn = MyDatagrid.Currentcell.ColumnNumber
Dim MyTextBox as textbox =CType(
MyDatagrid.TableStyles( said:
olumn), DataGridTextBoxColumn ).textbox

Regards,
Jan

Moogy said:
This might work; how do I return the DataGridTextBox of the selected cell?
Technically I guess I just need to get the object from the style of the
selected column; is this what you are recommending?

MOOGY!

Jan said:
Moogy,

I suggest you follow Miha's advice of using Row/ColumnChanging/ed. However
if you really insist, and provided you only use textfields and no checkboxes
in the datagrid, there is always the TextBox property of the
DataGridTextBoxColumn object, which returns you the textbox in use inside
the datagrid for the field you are editing. This textbox has the usual
events like TextChanged, KeyPress, KeyUp, KeyDown....

Regards,
Jan


"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
Hi Moogy,

This makes no sense; the datagrid has to know if the actual data has
changed; this is apparent in its behavior for type checking, etc.

When the cell is changed, it's obvious that an event is being
fired
 

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