Reputation: 1211
This one has stumped me now for a little bit. I am creating a derived System.Windows.Forms.DataGridView
control with some sensible default values. Now to make this clear they are simply default values and should be capable of being changed in the designer. As of right now I have the default values being set in the controls constructor:
using System.Drawing;
using System.Windows.Forms;
namespace TruckSmart.Controls
{
/// <summary>
/// An implementation of a System.Windows.Forms.DataGridView with sensible default values.
/// </summary>
public class DataGrid : System.Windows.Forms.DataGridView
{
/// <summary>
/// Initializes a new instance of the TruckSmart.Controls.DataGrid class.
/// </summary>
public DataGrid()
{
AllowUserToAddRows = false;
AllowUserToDeleteRows = false;
AllowUserToOrderColumns = false;
AllowUserToResizeColumns = false;
AllowUserToResizeRows = false;
AutoGenerateColumns = false;
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
BackgroundColor = SystemColors.Window;
BorderStyle = BorderStyle.Fixed3D;
ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
Margin = new Padding(0);
MultiSelect = false;
ReadOnly = true;
RowHeadersVisible = false;
SelectionMode = DataGridViewSelectionMode.FullRowSelect;
AlternatingRowsDefaultCellStyle = new DataGridViewCellStyle()
{
BackColor = Color.FromKnownColor(KnownColor.AliceBlue)
};
ColumnHeadersDefaultCellStyle = new DataGridViewCellStyle()
{
WrapMode = DataGridViewTriState.False
};
}
}
}
The main problem with using the constructor is that when I close my form designer in Visual Studio and re-open it the constructor is called again and any changes I made previously in the designer are reset. Is there a special place I should be putting these default values?
Upvotes: 2
Views: 1017
Reputation: 81620
Unfortunately, you will have to re-implement the properties yourself again:
[DefaultValue(false)]
public new bool AllowUserToAddRows {
get { return base.AllowUserToAddRows; }
set { base.AllowUserToAddRows = value; }
}
The keyword new will override or shadow the base property, allowing you to change it then. The DefaultValue attribute does not "set" the value of the property, it's just used by the designer to determine whether or not to write the value to the designer file or not. You would still have to set your default values in the constructor.
Upvotes: 2