Calling AddNew on BindingContext

  • Thread starter Thread starter Greg
  • Start date Start date
G

Greg

When I call AddNew on BindingContext (datasource is a Datatable), and prior
to calling End or Cancel CurrentEdit, how can I 'see' the new DataRow? All
of my bound controls can 'see' it, however I need to modify it manually at
runtime. I have tried doing a Select on the DataTable, but none of the
DataViewRowState's seem to fit.
 
If you call EndCurrentEdit() on the BindingContext's
currency manager that should move the data from the
bindingcontext to your table/view.

this is how you get an instance of the currencymanager
assuming you're in the form's code behind

CurrencyManager curMgr = (CurrencyManager)
this.BindingContext[MyTable];

curMgr.EndCurrentEdit();

or

((CurrencyManager)this.BindingContext
[MyTable]).EndCurrentEdit();


HTH
 
Dan, thanks for the reply. I already know that calling EndCurrentEdit
adds the DataRow. My question is how can I see the DataRow before
calling EndCurrentEdit?
 
Greg,
Your instance of the currencymanager has a property called "List". You
should be able to cast that list into a DataTable/DataView or whatever
you're using. The List is an instance of the IList interface of the
underlying datasource.

assume the curMgr variable is an instance of the CurrencyManager class.

DataTable myTable = (DataTable)curMgr.List;

HTH
 
Dan, I was thing the CM somehow stored a ref to the DataRow. Thanks for
the 'how to'.
 
Here is what I did:

Dim drNewAddr As DataRow = Me.dsSearch.Tables("addr").NewRow()
For Each col As DataColumn In Me.dsSearch.Tables("addr").Columns

Debug.WriteLine(col.ColumnName & ": " & drNewAddr.Item(col))

Next

this displays all of the columns with empty values, as expected. The
DataRow's state is 'Detached'



Me.BindingContext(dsSearch, "names.NamesFirmAddrRelation").AddNew()

Dim cm As CurrencyManager = Me.BindingContext(dsSearch,
"names.NamesFirmAddrRelation")

Dim dv As DataView = DirectCast(cm.List, DataView)

For Each dr As DataRow In dv.Table.Rows

Debug.WriteLine(dr.RowState.ToString)

For Each col As DataColumn In dv.Table.Columns

If col.ColumnName <> "timestamp_column" Then

Debug.WriteLine(col.ColumnName & ": " & dr.Item(col))

End If

Next

Next

this displays all of the Unchanged DataRows, nothing else. So, where is
the detached DataRow?
 
Greg,
from looking at your code the only thing you're doing different from what
I'm doing is that you're calling the AddNew() from the BindingContext and
I'm calling it from the my instance of the CurrencyManager. I don't think
that would be the issue, but I guess it is worth a try. I'm not sure where
to direct you at this point because when I call the AddNew() method I see
that the List property of the CurrencyManager has 1 record in it. Which is
correct for my test.
 
Greg,
OK..now I see what you're doing. You are adding a row to the DataTable
manually. Then you're adding a row through the binding context. Correct me
if I'm wrong, but the problem is that it appears as if only 1 record is
showing up as added. The row that you added manually through the table is
not showing up in the currencymanager's list???

I've looked at your code a bit more and I see one difference between what I
have and you have. I'm binding directly to a DataView vs. a DataTable. I
tried adding a new record manually through the my DataTable, then through my
currencymanager (DataView) and 2 records are showing up.

HTH
 
Well, I have since discovered that the call to AddNew creates a DataRowView,
not a DataRow.

So, with this knowledge I can now see the DataRowView.
Dim cm As CurrencyManager = Me.BindingContext(dsSearch,
"names.NamesFirmAddrRelation")

Dim dv As DataView = DirectCast(cm.List, DataView)

Dim drView As DataRowView = dv.AddNew

The only problem now is, calling NewRow creates a New DataRow with the
correct default values. Calling AddNew creates a DataRowView with the
values from the current record. .
 
Well, only for testing (comparing AddNew to NewRow) am I calling NewRow.

Ideally, I'd like to call AddNew on CM only.

Doing this does return a DataRowView, however it's pk column is set to the
current row's pk column.

Say for example I am sitting on Position 2 and it's pk value is 'this
stinks' . I call AddNew, which creates a DataRowView marked as 'Current'.
All of it's columns are empty except the pk, which is set to 'this stinks'.
Now, calls to EndCurrentEdit blow up as 'this stinks' is already in the
datatable.
 
I've hacked around this by binding a textbox to the pk. Then, just prior to
calling EndCurrentedit I stick a default value in the pk's text box.
 
Greg,
Just thought I'd let you know that I am using the NewRow() method when
manually adding a row. We built a framework around all of our business
entities which consists of about 4 class libraries and about 4-5 levels deep
in inheritance. I'm trying to sift through thousands of lines of code to
help you here, but I keep forgetting to specify the specifics. Here is what
we're doing when we add a row manually.

_data is a private instance of a DataSet.
this._data.Tables[0].Rows.Add(this._data.Tables[0].NewRow());

When we add a new row through the CM it is done like this:

this._currencyMgr.AddNew();
 
Good stuff. We have a main form that Shows a Dialog Form for writes and
reads from a DataTable in our main DataSet.

I pass of the main forms binding context to the modal forms constructor.
Rather than pass a datarow around, I decided to call AddNew on the BC when
the user wants to add a new row to the datatable. Edits work great.
Deletes work great. For some reason, calls to AddNew set the 'new' row's pk
to the current row's pk. Again, my hack was to add a textbox on the modal
form the bind to the pk and hide it. Then, prior to EndCurrentEdit, i stick
'NewControl" in the textbox and that gets rid of the constraint exception.

Dan Normington said:
Greg,
Just thought I'd let you know that I am using the NewRow() method when
manually adding a row. We built a framework around all of our business
entities which consists of about 4 class libraries and about 4-5 levels deep
in inheritance. I'm trying to sift through thousands of lines of code to
help you here, but I keep forgetting to specify the specifics. Here is what
we're doing when we add a row manually.

_data is a private instance of a DataSet.
this._data.Tables[0].Rows.Add(this._data.Tables[0].NewRow());

When we add a new row through the CM it is done like this:

this._currencyMgr.AddNew();

Greg said:
Well, I have since discovered that the call to AddNew creates a DataRowView,
not a DataRow.

So, with this knowledge I can now see the DataRowView.
Dim cm As CurrencyManager = Me.BindingContext(dsSearch,
"names.NamesFirmAddrRelation")

Dim dv As DataView = DirectCast(cm.List, DataView)

Dim drView As DataRowView = dv.AddNew

The only problem now is, calling NewRow creates a New DataRow with the
correct default values. Calling AddNew creates a DataRowView with the
values from the current record. .
Which
is where
is Thanks
for
 
Back
Top