Binding Navigator/Binding Source/DataGridView - how to?

csharp , db United Kingdom
  • 10 years ago

    I thought this would be pretty easy but I have been trying for 4 days now.

     What I am trying to do is

    • Query a database
    • Place the query results into a DataSet
    • Set the Binding Source to that DataSet
    • Set the Binding Navigator's DataSource to the Binding Source
    • Set the DataGridView's DataSource to the Binding Source

    However I cannot get the Binding Navigator to actually "navigate" through the returned data. The Navigator appears on my form, but the controls do not work and the row count says 1 whereas the DataGridView has around 300+ rows.

    I do not know what I am doing wrong, I appreciate any help.

    Here is the relevant code.

    </P><FONT size=2> <P>SqlCommand all = new SqlCommand("SELECT IndexNumber, VesselName, Active, Note, Agent, CustomerCode, ShippingAgent, CustomerPO, DeliverylabelReport, LastUpdate, EmailAddress from TableIndex", conn);
    SqlDataAdapter adpt = new SqlDataAdapter(all);
    DataSet ds = new DataSet();
    adpt.Fill(ds, "TableIndex");
    dgvCompanies.DataSource = ds.Tables[0];</P> <P>BindingNavigator _navigator;
    BindingSource _bindsrc = new BindingSource();</P> <P>_bindsrc.Add(ds);</P> <P>_navigator = new BindingNavigator(true);
    _navigator.Parent = this;
    _navigator.BindingSource = _bindsrc;
    _navigator.Dock = DockStyle.Top;</P> <P>dgvCompanies.DataSource = _bindsrc;</P> <P mce_keep="true">&nbsp;</P> <P>

  • 10 years ago

     

    Hi,

     

    This is because the data binding is not correct.  Use BindingSource to bind data with the DataGridView as well as BindingNavigator:

     

    Me.BindingSource1.DataSource = ds.Tables(0)

          Me.DataGridView1.DataSource = Me.BindingSource1

          Me.BindingNavigator1.BindingSource = Me.BindingSource1

     

    Hope this helps.

     

    Regards,

    Vinay

    ComponentOne LLC.

    www.componentone.com

     

     

  • 10 years ago
    Thanks, I have searched high and low, it's working now.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“God could create the world in six days because he didn't have to make it compatible with the previous version.”