Answered BindingNavigator

  • Saturday, June 06, 2009 8:41 PM
     
     
    Hi, The binding navigator is a pretty cool tool. However unless it has data connected to it is disabled. Sometime I present data that is not bound but would still like to use the tool (rather than create my own) to navigate through (and heres an old term coming) the recordset. Does anyone know how to use it unbound or if not any chance of having an 'Unbound' mode of operation.

    Regards

    AssemblyWasMoreFun

Answers

  • Friday, June 12, 2009 8:41 AM
    Moderator
     
     Answered

    Hi AssemblyWasMoreFun,

    From your description, I think you only wanted to use the UI elements and operations of the BindingNavigator Control without binding any data source. You only need to do two things:

    1.      Enable the items of the BindingNavigator.

    2.      Cope with the Click events of the items.

     

    The code snippet below shows how to do that:

     

            private void Form1_Load(object sender, EventArgs e)

            {

                //Init data in the grid view.

                InitDataGridView();

                //Init navigator.

                InitBindingNavigator();

            }

     

            private void InitDataGridView()

            {

                this.dataGridView1.AllowUserToAddRows = false;

     

                this.dataGridView1.Columns.Add("id", "id");

                this.dataGridView1.Columns.Add("name", "name");

     

                this.dataGridView1.Rows.Add(2);

                this.dataGridView1.Rows[0].Cells["id"].Value = "01";

                this.dataGridView1.Rows[0].Cells["name"].Value = "Jim";

                this.dataGridView1.Rows[1].Cells["id"].Value = "02";

                this.dataGridView1.Rows[1].Cells["name"].Value = "Peter";

            }

     

            private void InitBindingNavigator()

            {

                //Eable the items

                foreach (ToolStripItem item in this.bindingNavigator1.Items)

                {

                    item.Enabled = true;

                }

     

                //Initialize the count and position

                this.bindingNavigatorCountItem.Text = String.Format("of({0})", this.dataGridView1.Rows.Count);

                this.bindingNavigatorPositionItem.Text = "0";

     

                this.dataGridView1.CurrentCellChanged += new EventHandler(dataGridView1_CurrentCellChanged);

     

                //Handle add and delete event

                this.bindingNavigatorAddNewItem.Click += new EventHandler(bindingNavigatorAddNewItem_Click);

                this.bindingNavigatorDeleteItem.Click += new EventHandler(bindingNavigatorDeleteItem_Click);

                this.bindingNavigatorMoveFirstItem.Click += new EventHandler(bindingNavigatorMoveFirstItem_Click);

                this.bindingNavigatorMoveNextItem.Click += new EventHandler(bindingNavigatorMoveNextItem_Click);

     

                //Handle other events

            }

     

            private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)

            {

                //Set the position

                this.bindingNavigatorPositionItem.Text = this.dataGridView1.CurrentCell.RowIndex.ToString();

            }

     

            private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)

            {

                //Add new item

                this.dataGridView1.Rows.Add();

                //Refresh the count

                this.bindingNavigatorCountItem.Text = String.Format("of({0})", this.dataGridView1.Rows.Count);

            }

     

            private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)

            {

                //Delete selected rows.

                DataGridViewRow[] delRows = new DataGridViewRow[this.dataGridView1.SelectedRows.Count];

                this.dataGridView1.SelectedRows.CopyTo(delRows, 0);

                foreach (DataGridViewRow row in delRows)

                {

                    this.dataGridView1.Rows.Remove(row);

                }

                //Refresh the count

                this.bindingNavigatorCountItem.Text = String.Format("of({0})", this.dataGridView1.Rows.Count);

            }

     

            private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)

            {

                //Select the first item

                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[0].Cells[this.dataGridView1.CurrentCell.ColumnIndex];

            }

     

            private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)

            {

                //Select the next item

                int index = this.dataGridView1.CurrentRow.Index + 1;

                index = index % this.dataGridView1.Rows.Count;

                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[index].Cells[this.dataGridView1.CurrentCell.ColumnIndex];

            }

     

    Let me know if this helps.
    Aland Li

     


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.

All Replies

  • Sunday, June 07, 2009 6:49 PM
     
     

    Hi - This question seems to be specifically related to BindingNavigator. You will want to ask this in Windows Forms Data Controls and Databinding forums.

    Thanks,
    Emre

  • Friday, June 12, 2009 8:41 AM
    Moderator
     
     Answered

    Hi AssemblyWasMoreFun,

    From your description, I think you only wanted to use the UI elements and operations of the BindingNavigator Control without binding any data source. You only need to do two things:

    1.      Enable the items of the BindingNavigator.

    2.      Cope with the Click events of the items.

     

    The code snippet below shows how to do that:

     

            private void Form1_Load(object sender, EventArgs e)

            {

                //Init data in the grid view.

                InitDataGridView();

                //Init navigator.

                InitBindingNavigator();

            }

     

            private void InitDataGridView()

            {

                this.dataGridView1.AllowUserToAddRows = false;

     

                this.dataGridView1.Columns.Add("id", "id");

                this.dataGridView1.Columns.Add("name", "name");

     

                this.dataGridView1.Rows.Add(2);

                this.dataGridView1.Rows[0].Cells["id"].Value = "01";

                this.dataGridView1.Rows[0].Cells["name"].Value = "Jim";

                this.dataGridView1.Rows[1].Cells["id"].Value = "02";

                this.dataGridView1.Rows[1].Cells["name"].Value = "Peter";

            }

     

            private void InitBindingNavigator()

            {

                //Eable the items

                foreach (ToolStripItem item in this.bindingNavigator1.Items)

                {

                    item.Enabled = true;

                }

     

                //Initialize the count and position

                this.bindingNavigatorCountItem.Text = String.Format("of({0})", this.dataGridView1.Rows.Count);

                this.bindingNavigatorPositionItem.Text = "0";

     

                this.dataGridView1.CurrentCellChanged += new EventHandler(dataGridView1_CurrentCellChanged);

     

                //Handle add and delete event

                this.bindingNavigatorAddNewItem.Click += new EventHandler(bindingNavigatorAddNewItem_Click);

                this.bindingNavigatorDeleteItem.Click += new EventHandler(bindingNavigatorDeleteItem_Click);

                this.bindingNavigatorMoveFirstItem.Click += new EventHandler(bindingNavigatorMoveFirstItem_Click);

                this.bindingNavigatorMoveNextItem.Click += new EventHandler(bindingNavigatorMoveNextItem_Click);

     

                //Handle other events

            }

     

            private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)

            {

                //Set the position

                this.bindingNavigatorPositionItem.Text = this.dataGridView1.CurrentCell.RowIndex.ToString();

            }

     

            private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)

            {

                //Add new item

                this.dataGridView1.Rows.Add();

                //Refresh the count

                this.bindingNavigatorCountItem.Text = String.Format("of({0})", this.dataGridView1.Rows.Count);

            }

     

            private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)

            {

                //Delete selected rows.

                DataGridViewRow[] delRows = new DataGridViewRow[this.dataGridView1.SelectedRows.Count];

                this.dataGridView1.SelectedRows.CopyTo(delRows, 0);

                foreach (DataGridViewRow row in delRows)

                {

                    this.dataGridView1.Rows.Remove(row);

                }

                //Refresh the count

                this.bindingNavigatorCountItem.Text = String.Format("of({0})", this.dataGridView1.Rows.Count);

            }

     

            private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)

            {

                //Select the first item

                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[0].Cells[this.dataGridView1.CurrentCell.ColumnIndex];

            }

     

            private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)

            {

                //Select the next item

                int index = this.dataGridView1.CurrentRow.Index + 1;

                index = index % this.dataGridView1.Rows.Count;

                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[index].Cells[this.dataGridView1.CurrentCell.ColumnIndex];

            }

     

    Let me know if this helps.
    Aland Li

     


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.