3

When designing app in Visual Studio (C#), if I know that I will have a certain number of DataGridViews that all have same properties (like width, height, color, some other properties like: disable option to directly edit rows, etc.) is it ok to make my own class ("myDataGridView") that inherits DataGridView class and make all adjustments there, and then just instantiate that class later in code? Like this:

//my class:
class myDataGridView : DataGridView 
{
    this.BorderStyle = <someValue>
    this.ColumnCount = <someValue>
    //etc.

    public void method1() 
    {
    //some code...
    }
    public void method2() 
    {
    //some code...
    }
}

//instantiate it somewhere:
myDataGridView dgv1 = new myDataGridView();
myDataGridView dgv2 = new myDataGridView();
myDataGridView dgv3 = new myDataGridView();

Is that ok regarding OO principles? My friend says that putting code like

this.BorderStyle = <someValue>

in myDataGridView class is bad practice because adjusting properties like that will overrun properties of dataGridView which some other developer may adjusted visually in Visual Studio, if you know what I mean. Does that matter? I mean, if I want to treat my DataGridView as an object then it can have its properties and behavior, right? And having code that adjusts properties of DataGridView in my class is ok, readable, and every other developer who wants to change some properties can change it in myDataGridView class. It that kind of practice bad or wrong? If it is, what is the best practice when you know your app will have many DataGridViews which have same properties/behaviour? Thank you.

Robert Gowland
  • 7,677
  • 6
  • 40
  • 58
DCH
  • 125
  • 6

2 Answers2

6

In my opinion, this is not the right approach. Inheriting controls just for few property modifications does not make sense. You're not introducing any additional behavior in the control, Rather,try writing some initialization helper methods to do this.

this. __curious_geek
  • 42,787
  • 22
  • 113
  • 137
  • If you could tell us more about your objective, then fellow developers can come up with a good design idea for you. – this. __curious_geek Mar 14 '11 at 16:55
  • Ok, for example, I have four data tables in my SQL database. For every table I have one DataGridView. I wanted to make myDataGridView class that inherits DataGridView class (here are all adjustments of properties which need to be same for every DataGridView), and then I wanted to make four more classes (myDataGridViewTable1, myDataGridViewTable2...) that inherit that class (myDataGridView). Each of those four classes would have their own behaviour, like update(), delete(), get(), etc. Those are using stored procedures in database. So, I could use, for example, dataGridView1.update(); – DCH Mar 14 '11 at 18:47
1

If you are worried that your modifications to the properties get overwritten by a visual setting by the developer you may hide them using [Browsable(false)] attribute.

However, from OOP perspective inheritance is not encouraged unless really necessary. There is a golden rule : "Favor composition over inheritance". As this. __curious_geek said it's not a good idea to inherit controls to set properties this way but it's good practice to always have common base class for your business classes and screens. You may make a function in the base class of all screens to properly adjust the grids for you the way you want. If you make it virtual, you may override it in derived classes to customize the behavior per case as needed.

M.Sameer
  • 3,072
  • 1
  • 24
  • 37