Defaults in datagridview

pettrer

Well-known member
Joined
Sep 5, 2008
Messages
92
Programming Experience
10+
Hi,

I'm used to asp.net and skins + css for setting defaults for all my gridviews. Is there a simple way to accomplish the same thing for the datagridviews in winforms?

Properties like these are what I'm trying to keep in only one place, instead of setting them in the Properties pane or in each form's .VB file (like below):

VB.NET:
            gvTerm.ReadOnly = True
            gvTerm.RowHeadersVisible = False
            gvTerm.SelectionMode = DataGridViewSelectionMode.FullRowSelect
            gvTerm.ScrollBars = ScrollBars.Vertical
            gvTerm.AllowUserToResizeRows = False
            gvTerm.MultiSelect = False

Thanks for all input!
Pettrer
 
You could write a method that does it for the specified DGV:
VB.NET:
Sub SetDGVDefaults(DGV As DataGridView)
    DGV.ReadOnly = True
End Sub
 
Hi John,

Thanks for your reply. Just to clarify - is there no way of keeping defaults for datagridviews in one place?! :confused:

I guess that CSS-like properites can also only be set per each datagridview (gridcolor etc)?

If so, I can understand why the winforms projects I've seen all look so dull...;)

Please let me know if I'm missing something obvious here!

Pettrer
 
Have you tried creating your own control inheriting from System.Windows.Forms.DataGridView and setting your default properties in the constructor?
 
Have you tried creating your own control inheriting from System.Windows.Forms.DataGridView and setting your default properties in the constructor?

Hi,

Thanks for your suggestion. I've searched the net for info on this but it seems very hard to do. I'm also a bit worried about compatibility issues later on if we go our own way.

What we have is a bit over 200 (win-)forms in VB6 that are going to be upgraded to VB.Net 2.0. I'd like a neat way to make these forms, and in particular their datagridviews, uniform in their behaviour. For example, I now set AllowUsersToResizeRows to false for each one of the datagridviews and it's not only tedious work but also not risk-free. (In addition, appearance-like things such as backgroundcolor would also be nice to change in one place only (as in .css files).)

In Asp.Net this would have been a piece of cake (using .skin + .css files) but here I don't know. If it's easy/unproblematic to do as you suggest, I'd be very grateful for a link to a good source!

Thanks again,

Pettrer, Sweden
 
Just to clarify - is there no way of keeping defaults for datagridviews in one place?! :confused:
The suggested method is keeping defaults in one place, and you call this method only one time to apply the defaults to any given DGV.
 
The suggested method is keeping defaults in one place, and you call this method only one time to apply the defaults to any given DGV.

Hi again John,

Thanks for clarifying the theory, but for some reason I still can't find any info on how to actually accomplish this task. My questions are:

1. Where (in what file) do I put this sub (I presume it's the one you wrote above)?

2. How do I call it, in case I _do_ have to call it (I guess I shouldn't have to as it's supposed to be the default behaviour ;-) )?

Thanks for taking your time!

Pettrer
 
1. you can put it anywhere you want and arrage it any way you want, for example you can make it a shared method in your utilities class.
2. if you for example have a DGV1 and DGV2 you call SetDGVDefaults(DGV1) and SetDGVDefaults(DGV2).

You can also as suggested add it to a derived DataGridView class and make it an instance method with no parameter.
 
Hi again,

I read somewhere that messing with the defaults is not advisable (and I had no luck really doing it anayway) so I did as JohnH proposed.

This is what I have now.

In my .vb file with my commonly used subs, I have this:

VB.NET:
 Sub DGV1(ByVal DGV As DataGridView)
        DGV.AllowUserToResizeColumns = False
        DGV.AllowUserToResizeRows = False
        DGV.BackgroundColor = System.Drawing.Color.Silver
        DGV.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
        DGV.MultiSelect = False
        DGV.RowHeadersVisible = False
        DGV.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        DGV.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
    End Sub
In every form's Load sub, I have this code (it's the last thing in the Load sub as the datagridviews are not databound, thus populated and drawn in the Load sub):
VB.NET:
    commonStuff.DGV1(DataGridView1)
 End Sub

It works (of course, as it's just a matter of passing values via another sub). But isn't this very inefficient? I guess that these settings are written over and over again. In Asp.Net I would have written something like

VB.NET:
If NotPage.IsPostback Then 
    commonStuff.DGV1(DataGridView1)
 End If

to at least minimise the server work. Am I wrong, or is it neglectable work load anyway?

I guess a good result from doing this in this way is that I can easily change default background color or whatever in the future in just one place (as in css), as opposed to creating a custom control, which I guess would have hard-coded values for each and every datagridview, n'est-ce pas?

Another benefit is that, as JohnH pointed out, I can have a DGV2 sub for datagridviews that should have other preferences.

Please tell me if I'm doing it the wrong way!

Kind regards,

Pettrer
 
Windows Forms don't "postback", they "runat" client, the Load event is only ever called once for each instance.
 
Back
Top