Menu

How to draw on the column, instead of just showing a text or a combobox?

YourGod
2017-02-07
2017-02-07
  • YourGod

    YourGod - 2017-02-07

    I want to put a progress bar on a column. The in-built columns seem to support only text (with a checkbox and an icon) and combobox. I need more. How can I draw on a column myself?

    I want to achieve something like this.

    Or this.

     

    Last edit: YourGod 2017-02-07
  • YourGod

    YourGod - 2017-02-07

    After investigating the source code, I created a custom Control like the following and achieved what I wanted. I am adding this to help people in the future having the same requirements as I did.

    The treeColume2 was bound to Progress property which had a value ranging from 0 to 100.

    ProgressNode progNode = new ProgressNode();
    progNode.ParentColumn = treeColumn2;
    progNode.DataPropertyName = "Progress";
    treeViewAdv1.NodeControls.Add(progNode);

    class ProgressNode : BindableControl
    {
    static Size TestSize = new Size(200, 16);
    static SolidBrush BarBrush = new SolidBrush(Color.LightBlue);
    static SolidBrush LabelBrush = new SolidBrush(Color.Black);
    StringFormat CenterFormat =
    new StringFormat
    {
    Alignment = StringAlignment.Center,
    LineAlignment = StringAlignment.Center
    };

    public override void Draw(TreeNodeAdv node, DrawContext context)
    {
    var g = context.Graphics;
    var percentage = (int)GetValue(node);
    var fillRect = context.Bounds;
    int newWidth = fillRect.Width * percentage / 100;
    fillRect.Width = newWidth;

    g.FillRectangle(BarBrush, fillRect);
    g.DrawString($"{percentage}%", context.Font, LabelBrush, context.Bounds, CenterFormat);
    }

    public override Size MeasureSize(TreeNodeAdv node, DrawContext context)
    {
    return TestSize;
    }
    }

     

    Last edit: YourGod 2017-02-07

Log in to post a comment.