Groups | Blog | Home
all groups > dotnet windows forms databinding > november 2006 >

dotnet windows forms databinding : currently selected/active row syntax


Keith R
11/25/2006 10:00:43 PM
I'm stuck on this in two places of my code, and after struggling and
searching, I haven't figured it out yet.

I have two linked tables; a field in my master table becomes a linked field
in my second table (e.g. customerID, second table for customer orders uses
customerID in each entry). What is the SQL equivalent of "currently selected
row" of a given table, so I can add the ID to new entries?

I worked around this in a third case by adding a data field on a form, then
converting it back to INT to plug into my new row, but I also need to know
the "current" row so that I can provide an "oops" button to delete the last
added row in my child table. I'm assuming that the last added row is
considered "current" until another record is selected (or added).

Many thanks,
Keith

Keith R
11/26/2006 1:28:28 PM
An update, with code. I've been trying to pull snippets of code together to
make them work, and I'm stuck (again) due to my lack of .net and ADO sytax
understanding. This is for part of my original post (how to delete the last
record added to a table).

I have two tables in one database- MonkeyMain and MonkeyFlags. MonkeyMain
stores overall info about an AVI data clip (wild monkey video for research),
and MonkeyFlags stores information about content of the clip (behavior seen,
timestamp, and a foreign key back to the AVI Clip_ID). I'm trying to use
the following code to select the last added MonkeyFlags record and delete it
(in case a flag is added accidently, so the user can just delete it on the
fly). one concern I haven't addressed yet is whether the user can sort the
datatable on the form, and whether that could make this code delete the
wrong row, but I'm not even there yet.

Friend Sub DeleteCurrentFlag()

'I think this is pulling a row from MonkeyMain, where I want it to pull (I
think) from MonkeyFlags
If (Me.MonkeyMainBindingSource.Count > 0) AndAlso
(Me.MonkeyMainBindingSource.Current IsNot Nothing) Then
Dim rowView As DataRowView = CType(Me.MonkeyMainBindingSource.Current,
DataRowView)

'this is where it dies with invalidcastexception saying it can't cast a
monkeymain row to a monkeyflags row
Dim FlagRow As MonkeyMasterTableDataSet.MonkeyFlagsRow = CType(rowView.Row,
MonkeyMasterTableDataSet.MonkeyFlagsRow)

Dim msgText As String = String.Format("Are you sure you want to permanently
delete {0} from your collection? ", title)
'display Yes/No prompt to the user and store the result
Dim result As MsgBoxResult = MsgBox(msgText, MsgBoxStyle.YesNo, "OK to
delete?")
'remove the current movie if the user accepts the prompt
If result = MsgBoxResult.Yes Then
'so if I am linked to MonkeyMain and not MonkeyFlags, then this will delete
a row from the wrong table!
Me.MonkeyMainBindingSource.RemoveCurrent()
End If

' Make sure we always have a row available
If MonkeyMainBindingSource.Count = 0 Then
Me.MonkeyMainBindingSource.AddNew()
End If

End If
End Sub


[quoted text, click to view]

Keith R
11/26/2006 7:15:09 PM
Bart-
I had the two bindings (although I didn't know it), probably from all of my
other random testing to try to get it to work. I did try the line of code
you offered, and wow is it simpler than what I had. Unfortunately, it
appears that when I add records to my Flags table, the most recently added
flag is not set as the selected row/record. I assume that with each record
added, I'll have to query how many records are in the table, and set the
selectedrow to that value?
Thank you!
Keith

[quoted text, click to view]


Keith R
11/26/2006 7:23:18 PM
Woot! got it :) Just found ".MoveLast"
Thank you!

[quoted text, click to view]

Bart Mermuys
11/26/2006 9:42:35 PM
Hi,

[quoted text, click to view]

So, if you open DataSet Schema Designer (double click on your DataSet inside
Solution Explorer), you should see your two tables and two TableAdapters,
but you also need to add a relation (both relation and foreign key
constraint) between the tables: eg. MainToFlags.

Notice that once you added the right relation, inside DataSource's window
the Flags table will also be shown as a field in the Master table.

Then on the Form, you should have two BindingSource's :

MainBindingSource.DataSource = yourDataSet;
MainBindingSource.DataMember = "MonkeyMain";

FlagsBindingSource.DataSource = MainBindingSource;
FlagsBindingSource.DataMember = "MainToFlags"; // name of relation

The FlagsBindingSource will automatically be filtered based on
MainBindingSource.Current, and the FlagsBindingSource will automatically
insert the right foreign key for new flag records.

Then you can bind either simple Controls or a DataGridView to the
BindingSource's, offcourse if you bind to simple Conrols like TextBox you'll
also need one or two BindingNavigator to browse the records.

Then if you want to delete the current Flag, you use
FlagsBindingSource.RemoveCurrent();

Also see:
http://msdn2.microsoft.com/en-us/library/803kw7az(VS.80).aspx

HTH,
Greetings

[quoted text, click to view]

Bart Mermuys
11/27/2006 12:37:55 AM
Hi,

[quoted text, click to view]

If you add it directly to the DataTable, then yes, but the BindingSource
actually uses a DataView of the DataTable, the DataView can be sorted and
filtered, so it's not safe to assume the last added row will also be the
last row in the BindingSource(or DataView).

You could select the last added row using its key (assuming it has one):
FlagsBindingSource.Position = FlagsBindingSource.Find("keycolumn",
KeyOfLastAddedRow);

OR you could add the new row using the BindingSource (then the new record
will be current):
DataRowView drv = (DataRowView)FlagsBindingSource.AddNew();
drv["somefield1"] = somevlaue;
drv["somefield2"] = someothervalue;
...
drv.EndEdit();


HTH,
Greetings

[quoted text, click to view]

Bart Mermuys
11/27/2006 6:55:51 PM
Hi,

[quoted text, click to view]

Keep in mind, that MoveLast ins't (necesairly) the same as move to last
added item. My warning in my other reply still applies.

HTH,
Greetings


[quoted text, click to view]

AddThis Social Bookmark Button