2

How do I change the datagrid defaults for a program

 2 years ago
source link: https://www.codesd.com/item/how-do-i-change-the-datagrid-defaults-for-a-program.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How do I change the datagrid defaults for a program

advertisements

In my current project I am trying to find a way to reset the default style for all the datagridviews in our project. I was trying to set this up in the designer but the changes only stay for the first build and then are overwritten.

The end goal is that I would have one common place set up where I could change font size, alignment, color, etc... and then those changes would appear across the entire program. This would keep all the datagridviews in our project consistent without having to recopy the values 20 times for each separate datagridview.

Update: The method of loading the datagridview was changed such that instead of setting all the data up in a dataset, then passing that dataset to the gridview we now pass the datagridview to the method (With all the column settings done in the designer)and just load up the rows in the method. (Much 'cleaner' solution, went from 30 lines of code to 1 line of code to load a row)


You could sub class the DataGridView class and set the properties you would like as the new defaults in the constructor. Then instead of using the built in DataGridView you can use your new one.

public class MyGrid :DataGridView
{
    public MyGrid()
    {
        //I'm not a designer :)
        this.BackgroundColor = Color.AliceBlue;
        this.ForeColor = Color.Red;

        //other properties...
    }
}

If you are only using this in one project you can add this class to that project but if not you could create a separate dll with the class in it (plus any other extended controls).

Back on your form, from the toolbox you can then right click and select Choose Items... If you browse to the dll that you built the control in (or exe if it's in your exe project) you can the tick the control you have made. This makes it very simple to add to the design surface:

H5nQp.png

And there's the control in the toolbox:

Q0tA0.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK