Hi,

I am having difficulties creating a derived class for
DisabledDataGridViewTextBoxColumn and cell when the VisualStyle is not
supported. Basically I am down to either using ControlPaint or Graphics
to achieve this. But with Graphics, I am not sure how I can make them
diabled.

Below are my codes:

class DataGridViewDisabledTextBoxColumn : DataGridViewTextBoxColumn
{
public DataGridViewDisabledTextBoxColumn() {
this.CellTemplate = new DataGridViewDisabledTextBoxCell();

}
}
public class DataGridViewDisabledTextBoxCell :
DataGridViewTextBoxCell {
private bool enabledValue;
public bool Enabled {
get {
return enabledValue;
}
set {
enabledValue = value;
}
}

// Override the Clone method so that the Enabled property is
copied.
public override object Clone() {
DataGridViewDisabledTextBoxCell cell =
(DataGridViewDisabledTextBoxCell)base.Clone();
cell.Enabled = this.Enabled;
return cell;
}

// By default, enable the textbox cell.
public DataGridViewDisabledTextBoxCell() {
this.enabledValue = true;
}

protected override void Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates elementState, object value,
object formattedValue, string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts) {
// The button cell is disabled, so paint the border,
// background, and disabled button for the cell.
if (!this.enabledValue) {
// Draw the cell background, if specified.
if ((paintParts & DataGridViewPaintParts.Background) ==
DataGridViewPaintParts.Background) {
SolidBrush cellBackground =
new SolidBrush(cellStyle.BackColor);
graphics.FillRectangle(cellBackground, cellBounds);
cellBackground.Dispose();
}

// Draw the cell borders, if specified.
if ((paintParts & DataGridViewPaintParts.Border) ==
DataGridViewPaintParts.Border) {
PaintBorder(graphics, clipBounds, cellBounds,
cellStyle,
advancedBorderStyle);
}

// Calculate the area in which to draw the textbox.
Rectangle textBoxArea = cellBounds;
Rectangle textBoxAdjustment =
this.BorderWidths(advancedBorderStyle);
textBoxArea.X += textBoxAdjustment.X;
textBoxArea.Y += textBoxAdjustment.Y;
textBoxArea.Height -= textBoxAdjustment.Height;
textBoxArea.Width -= textBoxAdjustment.Width;

// Draw the disabled textbox

if
(System.Windows.Forms.VisualStyles.VisualStyleRenderer.IsSupported &&
Application.RenderWithVisualStyles) {
if
(System.Windows.Forms.VisualStyles.VisualStyleRenderer.IsElementDefined(System.Windows.Forms.VisualStyles.VisualStyleElement.Button.PushButton.Normal))
{
TextBoxRenderer.DrawTextBox(graphics,
textBoxArea, System.Windows.Forms.VisualStyles.TextBoxState.Disabled);

// Draw the disabled text.
if (this.FormattedValue is String) {
TextRenderer.DrawText(graphics,
(string)this.FormattedValue,
this.DataGridView.Font,
textBoxArea, SystemColors.GrayText);
}
}
} else {
Brush brsh = new SolidBrush(Color.Gray);
Brush brFill = new SolidBrush(Color.LightGray);
Pen p = new Pen(brsh,1);
graphics.DrawRectangle(p, textBoxArea);
graphics.FillRectangle(brFill, textBoxArea);
graphics.DrawString((string)this.FormattedValue,
this.DataGridView.Font, brsh, textBoxArea);
}
} else {
base.Paint(graphics, clipBounds, cellBounds,
rowIndex,
elementState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, paintParts);
}
}
}

I would appreciate if anyone can point me out here.

Thanks,

Sunny

Re: Disabled DataGridView Textbox column by VJ

VJ
Tue May 09 09:32:21 CDT 2006

tricky situation.. here is a very resourceful link..

http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx

see if any can help you..

VJ

"Sunny" <sunny076@yahoo.com> wrote in message
news:1147131428.599487.213180@v46g2000cwv.googlegroups.com...
> Hi,
>
> I am having difficulties creating a derived class for
> DisabledDataGridViewTextBoxColumn and cell when the VisualStyle is not
> supported. Basically I am down to either using ControlPaint or Graphics
> to achieve this. But with Graphics, I am not sure how I can make them
> diabled.
>
> Below are my codes:
>
> class DataGridViewDisabledTextBoxColumn : DataGridViewTextBoxColumn
> {
> public DataGridViewDisabledTextBoxColumn() {
> this.CellTemplate = new DataGridViewDisabledTextBoxCell();
>
> }
> }
> public class DataGridViewDisabledTextBoxCell :
> DataGridViewTextBoxCell {
> private bool enabledValue;
> public bool Enabled {
> get {
> return enabledValue;
> }
> set {
> enabledValue = value;
> }
> }
>
> // Override the Clone method so that the Enabled property is
> copied.
> public override object Clone() {
> DataGridViewDisabledTextBoxCell cell =
> (DataGridViewDisabledTextBoxCell)base.Clone();
> cell.Enabled = this.Enabled;
> return cell;
> }
>
> // By default, enable the textbox cell.
> public DataGridViewDisabledTextBoxCell() {
> this.enabledValue = true;
> }
>
> protected override void Paint(Graphics graphics,
> Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
> DataGridViewElementStates elementState, object value,
> object formattedValue, string errorText,
> DataGridViewCellStyle cellStyle,
> DataGridViewAdvancedBorderStyle advancedBorderStyle,
> DataGridViewPaintParts paintParts) {
> // The button cell is disabled, so paint the border,
> // background, and disabled button for the cell.
> if (!this.enabledValue) {
> // Draw the cell background, if specified.
> if ((paintParts & DataGridViewPaintParts.Background) ==
> DataGridViewPaintParts.Background) {
> SolidBrush cellBackground =
> new SolidBrush(cellStyle.BackColor);
> graphics.FillRectangle(cellBackground, cellBounds);
> cellBackground.Dispose();
> }
>
> // Draw the cell borders, if specified.
> if ((paintParts & DataGridViewPaintParts.Border) ==
> DataGridViewPaintParts.Border) {
> PaintBorder(graphics, clipBounds, cellBounds,
> cellStyle,
> advancedBorderStyle);
> }
>
> // Calculate the area in which to draw the textbox.
> Rectangle textBoxArea = cellBounds;
> Rectangle textBoxAdjustment =
> this.BorderWidths(advancedBorderStyle);
> textBoxArea.X += textBoxAdjustment.X;
> textBoxArea.Y += textBoxAdjustment.Y;
> textBoxArea.Height -= textBoxAdjustment.Height;
> textBoxArea.Width -= textBoxAdjustment.Width;
>
> // Draw the disabled textbox
>
> if
> (System.Windows.Forms.VisualStyles.VisualStyleRenderer.IsSupported &&
> Application.RenderWithVisualStyles) {
> if
> (System.Windows.Forms.VisualStyles.VisualStyleRenderer.IsElementDefined(System.Windows.Forms.VisualStyles.VisualStyleElement.Button.PushButton.Normal))
> {
> TextBoxRenderer.DrawTextBox(graphics,
> textBoxArea, System.Windows.Forms.VisualStyles.TextBoxState.Disabled);
>
> // Draw the disabled text.
> if (this.FormattedValue is String) {
> TextRenderer.DrawText(graphics,
> (string)this.FormattedValue,
> this.DataGridView.Font,
> textBoxArea, SystemColors.GrayText);
> }
> }
> } else {
> Brush brsh = new SolidBrush(Color.Gray);
> Brush brFill = new SolidBrush(Color.LightGray);
> Pen p = new Pen(brsh,1);
> graphics.DrawRectangle(p, textBoxArea);
> graphics.FillRectangle(brFill, textBoxArea);
> graphics.DrawString((string)this.FormattedValue,
> this.DataGridView.Font, brsh, textBoxArea);
> }
> } else {
> base.Paint(graphics, clipBounds, cellBounds,
> rowIndex,
> elementState, value, formattedValue, errorText,
> cellStyle, advancedBorderStyle, paintParts);
> }
> }
> }
>
> I would appreciate if anyone can point me out here.
>
> Thanks,
>
> Sunny
>