0

I have inherited a devexpress GridControl, following all steps from here.

There is only one problem, the method CreateDefaultView() is never called.

This is the code:

public partial class gttDXGridControl : GridControl
{
    public gttDXGridControl() : base()
    { }

    protected override void RegisterAvailableViewsCore(InfoCollection collection)
    {
        base.RegisterAvailableViewsCore(collection);
        collection.Add(new gttDXGridViewInfoRegistrator());
    }

    //this event is not called for some reason...
    protected override BaseView CreateDefaultView()
    {
        gttDXGridView gridView = CreateView("gttGridView") as gttDXGridView;
        // would like to initialize stuff here...
        return gridView;
    }
}

public partial class gttDXGridViewInfoRegistrator : DevExpress.XtraGrid.Registrator.GridInfoRegistrator
{
    public override string ViewName { get { return "gttGridView"; } }
    public override BaseView CreateView(GridControl grid) { return new gttDXGridView(grid as GridControl); }
    public override BaseViewInfo CreateViewInfo(BaseView view) { return new gttDXGridViewInfo(view as gttDXGridView); }
    public override BaseViewHandler CreateHandler(BaseView view) { return new gttDXGridHandler(view as gttDXGridView); }

}

public partial class gttDXGridViewInfo : GridViewInfo
{
    public gttDXGridViewInfo(DevExpress.XtraGrid.Views.Grid.GridView gridView) : base(gridView)
    { }

    public override int CalcRowHeight(Graphics graphics, int rowHandle, int min, int level, bool useCache, GridColumnsInfo columns)
    {
        return base.CalcRowHeight(graphics, rowHandle, MinRowHeight, level, useCache, columns);
    }

    public override int MinRowHeight
    {
        get { return base.MinRowHeight - 2; }
    }
}

This event is the place to be for initializing properties of the GridView, but its just never called. What could be the cause of this?

GuidoG
  • 11,359
  • 6
  • 44
  • 79

1 Answers1

1

This event is called when MainView property is null. It means that this event will be called when you add new gttGridControl to your form in design time or in runtime.
Here is example for runtime:

var gridControl = new gttDXGridControl();

gridControl.Width = 100;
gridControl.Height = 100;

Controls.Add(gridControl); // => Here this event will be invoked.
nempoBu4
  • 6,521
  • 8
  • 35
  • 40
  • So when I drop this control on a form this event should be called ? If so, then why is it not called in my gttDXGridControl ? – GuidoG Feb 04 '16 at 11:18
  • @GuidoG How you are checking this? – nempoBu4 Feb 04 '16 at 11:21
  • I have set a breakpoint in all my methods, the only method where the application does not stops is in this method. – GuidoG Feb 04 '16 at 11:37
  • @GuidoG Breakpoints are not working during design time. You can check it if you place something like `System.Windows.Forms.MessageBox.Show("Test");` inside your `CreateDefaultView` method. – nempoBu4 Feb 04 '16 at 11:39
  • I also had a line of code there to initialize a property of the gridview, this also did not happen – GuidoG Feb 04 '16 at 11:39
  • in runtime all this code also executed when loading the form, there the breakpoints do work, every breakpoint is called, except for this one – GuidoG Feb 04 '16 at 11:40
  • yes, as I already explained. I test all in runtime its the only way you can use breakpoints. Also, in the desinger.cs there is line of code : this.panelGrid.Controls.Add(this.gttDXGridList); – GuidoG Feb 04 '16 at 11:42
  • @GuidoG I have copypasted your code into new project and used my code in `Button.Click` event. Breakpoint is executed. – nempoBu4 Feb 04 '16 at 11:46
  • Have you inherited a new GridControl from devexpress GridControl like I did ? – GuidoG Feb 04 '16 at 11:48
  • but the breakpoint is only executed in the button click, not when the code from the designer.cs is executed ? Its exact the same code, so why does one gets executed and the other is not ? – GuidoG Feb 04 '16 at 11:52
  • @GuidoG Find in your `designer.cs` something like `gttDXGridControl1.MainView = gttGridView1;`, comment it and try to run. – nempoBu4 Feb 04 '16 at 11:54
  • but the GridControl in designtime now has a GridView in stead of a gttGridView. – GuidoG Feb 04 '16 at 12:05
  • hmm, after a second build the GridControl has the correct gttGridView in stead of the standard GridView. So now I now that this has something to do with my problem, the quesion is now, how to get around this. When dropping my GridControl on a form, this line of code is generated by visual studio. – GuidoG Feb 04 '16 at 12:12
  • @GuidoG The question is what are you really trying to achieve? There is possibility that `CreateDefaultView` method is not what you are looking for. – nempoBu4 Feb 05 '16 at 05:28