Grid Header

Pinned Featured

Comments

1 comment

  • Dapfor Team

    The .Net Grid supports merged columns. Using this feature the task becomes really easy. If you need to combine a column in one group, no special action is required. If you need to create a complex group composed of several others, in this case, you can draw merged column manually.

    Look at the provided example, demonstrating how to get the required result:

     

    public FormDemo()
    {
        InitializeComponent();

        Header header = grid.Headers[0];
        header.ColumnPanelHeight = 60;
        grid.Hierarchy.ButtonBehaviour = ExpansionButtonBehaviour.HideAlways;
        //header.StretchMode = ColumnStretchMode.All;

        header.Add(new Column("Contract"));
        header.Add(new Column("Segment"));
        header.Add(new Column("BuyQty", "Qty"));
        header.Add(new Column("BuyValue", "Value"));
        header.Add(new Column("SellQty", "Qty"));
        header.Add(new Column("SellValue", "Value"));
        header.Add(new Column("NetPositionQty", "Qty"));
        header.Add(new Column("NetPositionValue", "Value"));
        header.Add(new Column("OutstandingBuyQty", "Qty"));
        header.Add(new Column("OutstandingBuyValue", "Value"));
        header.Add(new Column("OutstandingSellQty", "Qty"));
        header.Add(new Column("OutstandingSellValue", "Value"));

        MergedColumn mc = header.MergedColums.CreateNew("OpenPosition");
        mc.Height = 40;
        mc.Add(header["BuyQty"]);
        mc.Add(header["BuyValue"]);
        mc.Add(header["SellQty"]);
        mc.Add(header["SellValue"]);

        mc = header.MergedColums.CreateNew("Net Position");
        mc.Height = 40;
        mc.Add(header["NetPositionQty"]);
        mc.Add(header["NetPositionValue"]);


        mc = header.MergedColums.CreateNew("OutstandingOrders");
        mc.Height = 40;
        mc.Add(header["OutstandingBuyQty"]);
        mc.Add(header["OutstandingBuyValue"]);
        mc.Add(header["OutstandingSellQty"]);
        mc.Add(header["OutstandingSellValue"]);

        grid.PaintMergedColumn += delegate(object sender, PaintMergedColumnEventArgs e)
        {
            if(e.Column.Text == "OpenPosition")
            {
                PaintMergedColumn(e, "Open Position", "Buy", "Sell");
            }
            if (e.Column.Text == "OutstandingOrders")
            {
                PaintMergedColumn(e, "OutStanding Orders", "Buy", "Sell");
            }
        };
    }

    private static void PaintMergedColumn(PaintMergedColumnEventArgs e, string topText, string bottomLeftText, string bottomRightText)
    {
        e.Parts ^= e.Parts & PaintPart.Text;
        e.PaintAll();
        e.Handled = true;

        Rectangle rcTop = e.VirtualBounds;
        rcTop.Height = e.Column.Height/2;
        Rectangle rcBottomLeft = new Rectangle(rcTop.X, rcTop.Bottom, rcTop.Width/2, e.VirtualBounds.Height - rcTop.Height);
        Rectangle rcBottomRight = new Rectangle(rcTop.X + rcBottomLeft.Width, rcTop.Bottom, e.VirtualBounds.Width - rcBottomLeft.Width, e.VirtualBounds.Height - rcTop.Height);

        e.Render.DrawCaption(e.Appearance, ElementState.Active, rcTop, BorderSide.All, e.Graphics);
        e.Render.DrawCaption(e.Appearance, ElementState.Active, rcBottomLeft, BorderSide.All, e.Graphics);
        e.Render.DrawCaption(e.Appearance, ElementState.Active, rcBottomRight, BorderSide.All, e.Graphics);

        using (StringFormat sf = new StringFormat())
        {
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            e.Graphics.DrawString(topText, e.Font, SystemBrushes.ControlText, rcTop, sf);
            e.Graphics.DrawString(bottomLeftText, e.Font, SystemBrushes.ControlText, rcBottomLeft, sf);
            e.Graphics.DrawString(bottomRightText, e.Font, SystemBrushes.ControlText, rcBottomRight, sf);
        }
    }

     

    Best regards,

    Dapfor

Please sign in to leave a comment.

Powered by Zendesk