Thanks for the reply.
I've tried a couple of things before and since your post. Here's my
attempt to implement one of your suggestions:
Your suggestion was:
myDataSet.MyTable[currencyMgr.Position]["field"] = someValue;
and my implementation was:
dsTaskActivities.Tables["Tasks"][cMgr.Position]["SubCategory"] =
Value;
The error message received was:
Cannot apply indexing with [] to an expression of type
'System.Data.DataTable'
So I changed it to this:
dsTaskActivities.Tables["Tasks"].Rows[cMgr.Position]["SubCategory"] =
Value;
And added this:
dsTaskActivities.Tables["Tasks"].Rows[cMgr.Position].EndEdit();
Now here's the real problem. I've realized through some experimentation
that the problem lies in the specific control that I'm using. I'm using
a ComboBox to allow the user to select a value from a foreign key table.
When a selection is made, the selected value is revealed in a TextBox
that I added for debugging purposes; so I know that the value is being
set properly. When I leave the editted row and return, the new value is
still in the TextBox but the ComboBox is showing another value.
Sometimes it's the previous value, sometimes it's something else
entirely. While I thought that I was having trouble getting the value
selected by the user to stick, it appears that I'm really having trouble
getting the ComboBox to show the current value. I've used the following:
cbSubCategoryLookUp.DataBindings.Add("Text", dsTaskActivities,
"Tasks.SubCategory");
textBox2.DataBindings.Add("Text", dsTaskActivities, "Tasks.SubCategory");
And yet they don't always show the same value even though the ComboBox is
bound to the table that holds the only values allowed in SubCategory and
the only values that show up in the TextBox are selected from the
ComboBox dropdown list. This is how the ComboBox is bound:
cbSubCategoryLookUp.DataSource = dsCatSubCats.Tables["Category"];
cbSubCategoryLookUp.DisplayMember = "CatSubCats.SubCategory";
Do you have any idea what I'm missing here?
Chris.
Ian Griffiths said:
Christopher Weaver wrote:
I want to set a value in a specific field in the current row of a
DataSet.
So the first thing to know is that the DataSet doesn't have any notion
of a current row.
In fact strictly speaking, the DataSet doesn't have any notion of a row.
It just contains a bunch of DataTables. And it's the DataTables that
contain the rows. But a DataTable doesn't have any notion of a current
row either, no more than a table in a relational database has a notion
of a 'current' row.
Currency (in the sense of what is currently selected) is entirely a UI
thing. It's managed by the currency manager in a Windows Forms
applciation.
SomeRow["fieldName"] = 'value';
But how do I set SomeRow to the row that the user is currently viewing?
I tried this:
DataRowView drv = (DataRowView) cMgr.Current; //Where cMgr is a
CurrencyManager
drv["SubCategory"] = Value;
But it doesn't stick. When the user moves off the record and back
again, the previous value is restored.
You mean that your changes aren't being stored in the DataTable?
In which case you've misdiagnosed the problem. You *are* successfully
getting hold of the current row. The reason the changes aren't sticking
is because you probably need to call EndEdit on the DataRowView.
In a Windows Forms app, edits are provisional until they are applied.
There are a number of reasons. One is that it may only make sense to
check constraints once all of the fields for a row have been entered, in
which case there has to be some point at which you say 'done now!' to
try and apply the changes.
Also, by convention, in most grid editing UIs in Windows, you can
usually cancel out of your current row edit.
So the changes you make through the view are probably not sticking
because the edit is being regarded as provisional, and you never try to
finish the edit.
At least that's one possibility - try calling EndEdit on the DataRowView
and see if that helps.
Alternatively, just get the Position from the CurrencyManager, and go
straight to the DataTable object rather than going via the view.
Something like:
myDataSet.MyTable[currencyMgr.Position]["field"] = someValue;