8
votes

Guys I am trying to implement paging in my datagridview through bindingnavigator in C# windows forms application.

I have simply dragged datagridview and bindingnavigator from Toolbar to form. Datagridview is databounded to a database table in SQL server using dataset. I have added 3 extra buttons to gridview that will be doing some function.

Now I have never used bindingnavigator before, so I just selected bindingsource of datagridview1 for datasource of bindingnavigator from its properties.

This is how my form looks upon running :

enter image description here

Currently, datagridview1 displays all the records in my table (31, as of now) and binding navigator next button just takes me to next record (for example, from TicketID=1 to TicketID=2).

Now, what I want to do is :

1.) Datagridview should only display 10(or 50) records per page, and bindingnavigator control should be used to switch between pages so that the UI remains responsive and it becomes more "memory-efficient" because eventually my database will have thousands of records.

2.) BindingNavigator's controls should appear in center of the form, not to the left/right. I couldn't set it to center from properties.

Code behind my form :

       using System;
       using System.Collections.Generic;
       using System.ComponentModel;
       using System.Data;
       using System.Drawing;
       using System.Linq;
       using System.Text;
       using System.Threading.Tasks;
       using System.Windows.Forms;

       namespace WindowsFormsApplication2
       {
           public partial class Form9 : Form
           {
               public Form9()
    {
        InitializeComponent();
    }

    private void Form9_Load(object sender, EventArgs e)
    {
        this.CenterToScreen();
        try
        {
            this.tblTicketDetailTableAdapter.Fill(this.sTDataSet4.tblTicketDetail);
        }
        catch
        {
            MessageBox.Show("Error : Cannot establish a valid connection to database.", "SQL SERVER ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == dataGridView1.Columns["Detail"].Index)
        {
            //some code
        }
        else if (e.ColumnIndex == dataGridView1.Columns["Close"].Index)
        {
            //some code
        }
        else if (e.ColumnIndex == dataGridView1.Columns["ViewDetail"].Index)
        {
            //some code
        }
    }

}

Now what can I do to make bindingnavigator work as paging control?

1
check the answer of this SO questionJunaith

1 Answers

8
votes

One way to do this is by breaking up the parent table into a list of DataTables (or my preference, BindingList<DataTable>) and then assigning the grid's DataSource on the position change of the navigator control:

BindingSource bs = new BindingSource();
BindingList<DataTable> tables = new BindingList<DataTable>();

protected override void OnLoad(EventArgs e) {
  base.OnLoad(e);

  int counter = 0;
  DataTable dt = null;
  foreach (DataRow dr in tblTicketDetail.Rows) {
    if (counter == 0) {
      dt = tblTicketDetail.Clone();
      tables.Add(dt);
    }
    dt.Rows.Add(dr.ItemArray);
    ++counter;
    if (counter > 9) {
      counter = 0;
    }
  }
  bindingNavigator1.BindingSource = bs;
  bs.DataSource = tables;
  bs.PositionChanged += bs_PositionChanged;
  bs_PositionChanged(bs, EventArgs.Empty);
}

void bs_PositionChanged(object sender, EventArgs e) {
  dataGridView1.DataSource = tables[bs.Position];
}

As far as centering the BindingNavigator control, just set the Dock style to None and position the control in the center manually. Once set, set the Anchors to none and the ToolBar should "float" in the center when the container is resized.