Search code examples
c#winformsbindingnavigator

Extending BindingNavigator


I want to extend the BindingNavigator so I can add extra functionality to it. One of the things I want to do is add a ToolStripSplitButton that will autosize the cells in a DataGridView. I've been able to add the button, but when I drop the control on a form, my button is in the first position. I would like to add this button after the Delete button. How can I do this?

Here is what the control looks like when dropped onto a form at design time: nav

Here is the code:

public class DataGridToolStrip : BindingNavigator
{

    private ToolStripSplitButton AutoSizeButton;
    private ToolStripMenuItem mnuAllCells;
    private ToolStripMenuItem mnuAllCellsExceptHeader;
    private ToolStripMenuItem mnuColumnHeader;
    private ToolStripMenuItem mnuDisplayedCells;
    private ToolStripMenuItem mnuDisplayedCellsExceptHeader;

    public DataGridToolStrip() : base(false)
    {
        //this.Items.Clear();
        //this.AddStandardItems();

        this.mnuAllCells = new ToolStripMenuItem();
        this.mnuAllCellsExceptHeader = new ToolStripMenuItem();
        this.mnuColumnHeader = new ToolStripMenuItem();
        this.mnuDisplayedCells = new ToolStripMenuItem();
        this.mnuDisplayedCellsExceptHeader = new ToolStripMenuItem();
        this.AutoSizeButton = new ToolStripSplitButton();

        this.AutoSizeButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.AutoSizeButton.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.mnuAllCells,
        this.mnuAllCellsExceptHeader,
        this.mnuColumnHeader,
        this.mnuDisplayedCells,
        this.mnuDisplayedCellsExceptHeader});

        this.AutoSizeButton.Name = "AutoSizeButton";
        this.AutoSizeButton.Size = new System.Drawing.Size(72, 22);
        this.AutoSizeButton.Text = "Auto Size";



        this.Items.Add(AutoSizeButton);
    }
}

Solution

  • You can override AddStandardItems method of BindingNavigator and and add additional items after calling base.AddStandardItems():

    public class DataGridToolStrip : BindingNavigator
    {
        public override void AddStandardItems()
        {
            base.AddStandardItems();
            // Add addtional items here
        }
    }