views:

56

answers:

1

I have created a user control (custom DataGridView control). I have used the example in this MSDN article to set the border style.

I am able to see the selected border style in designer. Like None, FixedSingle or Fixed3D.

But when I set the border style to FixedSingle, the border does not appear at runtime. Do I need to draw it manually in the OnPaint method?

If I use following code

private BorderStyle borderStyle = BorderStyle.None;

      [Browsable (true)]
      public new BorderStyle BorderStyle
      {
         get
         {
            return borderStyle;
         }

         set
         {
            if (borderStyle != value)
            {
               if (!Enum.IsDefined(typeof(BorderStyle), value))
               {
                  throw new InvalidEnumArgumentException("value", (int)value, typeof(BorderStyle));
               }

               base.BorderStyle = value;
               UpdateStyles();
            }
         }
      }

The border on designer but its size is fixed, its smaller than the grid size. Its size remain same even if I resize grid and the same border appears in runtime.

+1  A: 

That KB article is badly outdated, it talks about .NET 1.x. In .NET 2.0, UserControl got a BorderStyle property. It can be set to None, FixedSingle and Fixed3D. FixedSingle works fine when I try it, I never heard of a problem with it. Remove the CreateParams override.


using System;
using System.ComponentModel;
using System.Windows.Forms;

class MyDgv : DataGridView {
  public MyDgv() {
    base.BorderStyle = BorderStyle.None;
  }

  [Browsable(true)]
  [DefaultValue(BorderStyle.None)]
  public new BorderStyle BorderStyle {
    get { return base.BorderStyle; }
    set {
      if (base.BorderStyle != value) {
        base.BorderStyle = value;
        UpdateStyles();
      }
    }
  }

}
Hans Passant
If I remove the CreateParams override, the border does not get applied in design time as well.
Ram
So, did you actually change the BorderStyle property in the designer?
Hans Passant
No, I have not override it. I am deriving my control from system grid control. But I am doing lots of things in paint.
Ram
Erm, this doesn't have anything to do with UserControl then? DataGridView also has a BorderStyle property. What happened to it? What is the actual base class of your control?
Hans Passant
DataGridView class.
Ram
DataGridView has a BorderStyle property. Why doesn't it work? Are you overriding it and not setting the base.BorderStyle property? Time to post some code, I have no idea what you are doing.
Hans Passant
Please check the problem statement again. I have added the code there.
Ram
There's a bug in your setter, it doesn't update the "borderStyle" member. I updated my post with another version.
Hans Passant