Need help with BindingNavigator

M

Michael Russell

Hi, I'm looking for a simple way to scroll through records, and a
BindingNavigator seemed to be it. However, I can't seem to get it to
work. I can see in an output window that I'm retrieving the correct
records, and the BindingNavigator indicates that I have 2 records.
However, using the Next and Previous buttons in the Nav don't update the
controls on the form, they always (only) show the data in the fields of
the first record.

Using the following code:

public partial class Form1 : Form
{
BindingSource bs = new BindingSource();

public Form1() {
InitializeComponent();
}

private void Form1_Load( object sender, EventArgs e ) {
FarmDataSet fds = new FarmDataSet();
DataSet ds = fds.GetDataSet();

bs.DataMember = "farm";
bs.DataSource = ds;
navFarms.BindingSource = bs;

foreach ( DataTable tbl in ds.Tables ) {
foreach ( DataRow row in tbl.Rows ) {
Console.WriteLine( "{0} ({1}) row: {2}, {3}, {4}",
tbl.TableName, tbl.Rows.Count,
row.ItemArray[ 0 ],
row.ItemArray[ 1 ],
row.ItemArray[ 2 ] );
}
}

txtName.DataBindings.Add(
new Binding( "Text", ds, "farm.name", true ) );
lblFarm.DataBindings.Add(
new Binding( "Text", ds, "farm.name", true ) );

}
}

Any help is appreciated, Thanks, Michael
 
M

Michael Russell

Ok, I should mention, that I can get this to work (for the most part) by
implementing all the Click events for the BN. Looking at an example
from Microsoft, I didn't think I was supposed to have to do this, ie, I
thought the point of using the Navigator was that this part would be
autmatic.

Anyway, the problem I'm having now is that if I click the Delete button,
it appears to delete twice. I use the following code:

private void bindingNavigatorDeleteItem_Click( object sender,
EventArgs e)
{
Console.WriteLine( "Num rows before: {0}", bm.Count );
bm.RemoveAt( bm.Position );
Console.WriteLine( "Num rows after: {0}", bm.Count );
navFarmsNew.Refresh();
}

The output from the "Num rows before" indicates that I have one less
record than I expect. Specifically, using a DataSet of two records, I
can scroll back and forth between the two, but when I click Delete, the
first line of output is: "Num rows before: 1".

BTW, "bm" is used through, and defined as:
BindingManagerBase bm = this.BindingContext[ ds, "farm" ];

What am I missing here?
 
J

Jim Hughes

Bind the controls to the BindingSource, not the dataset

txtName.DataBindings.Add(
new Binding( "Text", bs, "name", true ) );
lblFarm.DataBindings.Add(
new Binding( "Text", bs, "name", true ) );
 
J

Jim Hughes

Because the BindingNavigator has a DeleteItem property that is set to the
control that is used to implement the delete. It also deletes the record.

If you want to handle it yourself, then clear the DeleteItem property and
handle the control's Click event.
 
M

Michael Russell

Jim said:
Bind the controls to the BindingSource, not the dataset

txtName.DataBindings.Add(
new Binding( "Text", bs, "name", true ) );
lblFarm.DataBindings.Add(
new Binding( "Text", bs, "name", true ) );

Thanks for that. As a follow-up question, is it possible to use a
BindingNavigator to scroll through information from two related tables?
For example, have TextBoxes for order details (from a detail table)
that can be changed, and a label from a Header table that displays a
customer name, while using the Navigator to scroll through the details?

Thanks
 

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