PC Review


Reply
Thread Tools Rate Thread

Can't update a DataTable after sorting and editing its dataview

 
 
=?Utf-8?B?RGFycnls?=
Guest
Posts: n/a
 
      23rd Jul 2005
Hi folks. Hoping for some help on a issue that has stalled my programming
efforts.

My app. presents a mailing list to the user and allows them to change the
status of the members of the mailing list (i.e. "Active", "Mail Returned",
"Requested Removal") etc.

I have a Access database, from which I create a DataSet with one table. I
create a DataView and set it as the DataSource to a DataGrid on my Windows
form.

I let the user sort by clicking on any column header. I capture the
DataGrid.MouseDown event and run a HitTest to see if the user clicked on a
HitTestType.ColumnHeader and if they did, I set the (DataView.Sort =
ColumnName. )

I've set the Status column to a custom GridColumnStyle which inherits from
DataGridTextBoxColumn and adds ComboBox functionality (cut and paste from an
internet example). The Combobox lets the user choose one of the pre-set
status values allowed.

Enough background, all that works fine. When the combobox changes its
value, it calls

>SetColumnValueAtRow(CurrencyManager, rowNum, ColumnComboBox.Text)


This changes the cell in the DataView. The correct value appears in the
DataGrid. However, the DataTable which underlies the DataView does not have
its cell updated. Next time I sort, the DataView sorts its rows based on the
DataTable (as expected), but the value in the DataView cell (which is
different) now appears out of sort order.

But that's just a symptom of the problem, because what I want to do is
change the value in the datatable and update my Access database with the new
value. The app's point is to let the user change the status of the
mailinglist members...

SO, my question is; How can I get the column to change the underlying
DataSet/DataTable?

Thanks everyone for your time and help.

Darryl.

 
Reply With Quote
 
 
 
 
W.G. Ryan MVP
Guest
Posts: n/a
 
      23rd Jul 2005
If you're using DataBinding, then as soon as you change the value through UI
controls and the edit is finished, the value in the underlying table is
changed. If you've 'changed' the value in the dataview, you've changed it
in the datatable - it may appear otherwise but if the value was actually
changed in the view, then it's definitely changed in the datatable as well.

So a few things: 1) How have you verified that the value is changed in the
DataView? If you successfully changed it in the view, then it's changed in
the DataTable. Typically, problems with the symptom you've described are
caused by improperly referencing the row index (so the 'real' value was
changed, but since you're referencing a different index, it looks like it's
not changed). Another problem comes from changing the value in the
DataTable, but not submitting the update successfully to the database...
then another select query is performed which retrieves the values from teh
DB. Since the values were changed in the datatable, but no successful
update was performed, when you requery it, you lose all the values you have.
3) Make sure that you're not eating any exceptions and not notifiying
yourself. If the update is going bad and you just eat the exception, then
the problem is w/ the Update on the back end.

As such, verify that hte row has changes RIGHT before you call update, and
verify that there are no changes afterward.

ie
Debug.Assert(MyDataSet.HasChanges(), "There are NO changes available but we
expect that there should be");
MyDataAdaptger.Update(MyDataSet, "TableName");
Debug.Assert(!MyDataSet.HasChanges(), "Ther are changes present and there
shouldn't be");

I'd throw in the assertions liberally before and after any of my 'edits' so
that I verified that chagnes were present (now the second assertion will
fail if you don't call update - so on your edits keep this in mind)

Let me know the results of the assertions and we'll take it from there.

Cheers,

Bill


"Darryl" <(E-Mail Removed)> wrote in message
news:473BE46E-34DA-461A-9809-(E-Mail Removed)...
> Hi folks. Hoping for some help on a issue that has stalled my programming
> efforts.
>
> My app. presents a mailing list to the user and allows them to change the
> status of the members of the mailing list (i.e. "Active", "Mail Returned",
> "Requested Removal") etc.
>
> I have a Access database, from which I create a DataSet with one table. I
> create a DataView and set it as the DataSource to a DataGrid on my Windows
> form.
>
> I let the user sort by clicking on any column header. I capture the
> DataGrid.MouseDown event and run a HitTest to see if the user clicked on a
> HitTestType.ColumnHeader and if they did, I set the (DataView.Sort =
> ColumnName. )
>
> I've set the Status column to a custom GridColumnStyle which inherits from
> DataGridTextBoxColumn and adds ComboBox functionality (cut and paste from
> an
> internet example). The Combobox lets the user choose one of the pre-set
> status values allowed.
>
> Enough background, all that works fine. When the combobox changes its
> value, it calls
>
> >SetColumnValueAtRow(CurrencyManager, rowNum, ColumnComboBox.Text)

>
> This changes the cell in the DataView. The correct value appears in the
> DataGrid. However, the DataTable which underlies the DataView does not
> have
> its cell updated. Next time I sort, the DataView sorts its rows based on
> the
> DataTable (as expected), but the value in the DataView cell (which is
> different) now appears out of sort order.
>
> But that's just a symptom of the problem, because what I want to do is
> change the value in the datatable and update my Access database with the
> new
> value. The app's point is to let the user change the status of the
> mailinglist members...
>
> SO, my question is; How can I get the column to change the underlying
> DataSet/DataTable?
>
> Thanks everyone for your time and help.
>
> Darryl.
>



 
Reply With Quote
 
=?Utf-8?B?RGFycnls?=
Guest
Posts: n/a
 
      25th Jul 2005
Hi Bill,

Well, thank you very much for your reply. 30 minuites after reading it I
had resolved the issue. Clarified and recitified my thinking on the subject.

I think the core of my problem was that I was treating DataView/DataTable
relationship the same way I treat the DataSet/Database relationship.
Basically, as a disconnected relationship. Even though I've read and been
told the DataView automatically updates its DataTable, somehow it wasn't
registering.

Because I had that custom combobox GridColumnStyle, I had it wired for an
event ("DataUpdated") which fired when the combobox fired its
selectionchangecomitted. This bubbled up to my form and I made an event
handler to do the database update. I was sure this would work. But when I
put in the Debug.Assert statements you recommended, I found the event was
firing before the DataTable was changed and so the Database wasn't being
updated.

I realised I don't understand the whole edit/commit process down in that
DataGridStyle, so I fiddled with it, trying to call my custom-built
DataUpdated event from different places in the code (the combobox's
SelectedIndexChange, the datagridstyle's Commit, etc) and I realized I was in
over my head.

After putting in your Debug.Assert statements, and testing for the
DataSet.HasChanges property, I realised the DataTable as a RowChanged event.
So I tore out my custom event for my custom GridColumnStyle. I left the
DataView, CurrencyManager, and DataTable to run on their default behaviours
and just added a handler for the DataTable.RowChanged event which called my
UpdateDatabase procedure, and that resolved the issue.

I blame my ASP background; this is my first foray into Windows forms, and I
lack some basic familiarity with the object model. I really appreciate your
help resolving the issue. My next assignment is to work on understanding that
edit/commit process in the controls...


Thanks again,

Darryl.


"W.G. Ryan MVP" wrote:

> If you're using DataBinding, then as soon as you change the value through UI
> controls and the edit is finished, the value in the underlying table is
> changed. If you've 'changed' the value in the dataview, you've changed it
> in the datatable - it may appear otherwise but if the value was actually
> changed in the view, then it's definitely changed in the datatable as well.
>
> So a few things: 1) How have you verified that the value is changed in the
> DataView? If you successfully changed it in the view, then it's changed in
> the DataTable. Typically, problems with the symptom you've described are
> caused by improperly referencing the row index (so the 'real' value was
> changed, but since you're referencing a different index, it looks like it's
> not changed). Another problem comes from changing the value in the
> DataTable, but not submitting the update successfully to the database...
> then another select query is performed which retrieves the values from teh
> DB. Since the values were changed in the datatable, but no successful
> update was performed, when you requery it, you lose all the values you have.
> 3) Make sure that you're not eating any exceptions and not notifiying
> yourself. If the update is going bad and you just eat the exception, then
> the problem is w/ the Update on the back end.
>
> As such, verify that hte row has changes RIGHT before you call update, and
> verify that there are no changes afterward.
>
> ie
> Debug.Assert(MyDataSet.HasChanges(), "There are NO changes available but we
> expect that there should be");
> MyDataAdaptger.Update(MyDataSet, "TableName");
> Debug.Assert(!MyDataSet.HasChanges(), "Ther are changes present and there
> shouldn't be");
>
> I'd throw in the assertions liberally before and after any of my 'edits' so
> that I verified that chagnes were present (now the second assertion will
> fail if you don't call update - so on your edits keep this in mind)
>
> Let me know the results of the assertions and we'll take it from there.
>
> Cheers,
>
> Bill
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does sorting a dataview cause its datatable to change =?Utf-8?B?SmVycnkgSg==?= Microsoft ADO .NET 1 4th May 2007 03:17 PM
datagrid dataset dataview datatable sorting Nathan Franklin Microsoft VB .NET 0 19th Jan 2006 05:15 AM
Sorting datagrid-dataview-datatable =?Utf-8?B?c3ViVA==?= Microsoft ADO .NET 2 27th May 2004 03:56 PM
Sorting DataTable without DataView maciek kanski Microsoft ADO .NET 2 10th Feb 2004 09:11 PM
dataGrid dataview sorting/editing Martin Schmid Microsoft C# .NET 2 1st Nov 2003 08:40 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:01 PM.