Help. Problem with MaskedTextBox in DataGridView control

A

AboutJAV

Hi,

I am adding a MaskedTextBox control to a datagridcontrol. So far, I
can see the masked value, but when I start the application up, the
maskedtextbox cell is blank. When I select it, I can see the masked
value "00000-000-00000-0". I change the value and click a readonly
cell. The maskedtextbox values goes blank. When I click on the cell
again, the value reappears on selection event. However, When I click
on a non readonly cell, the maskedtextbox value totally disappear.

At startup, the masked value 00000-000-00000-0 is invisible until I
select the value. Here is my code. Any help is greatly appreciated.


public class MaskedTextBoxCell : DataGridViewTextBoxCell
{

public MaskedTextBoxCell()
: base()
{
}


public override void InitializeEditingControl(int rowIndex,
object
initialFormattedValue, DataGridViewCellStyle
dataGridViewCellStyle)
{
try
{
// Set the value of the editing control to the current
cell value.
base.InitializeEditingControl(rowIndex,
initialFormattedValue,
dataGridViewCellStyle);
MaskedTextBoxEditingControl ctl =
DataGridView.EditingControl as
MaskedTextBoxEditingControl;
ctl.Mask = "00000-000-00000-0";
//ctl.Value = (DateTime)this.Value;
}
catch (System.ArgumentOutOfRangeException)
{
// do nothing
}
}

public override Type EditType
{
get
{
// Return the type of the editing contol that
CalendarCell uses.
return typeof(MaskedTextBoxEditingControl);
}
}

public override Type ValueType
{
get
{
return typeof(MaskedTextBox);
// Return the type of the value that CalendarCell
contains.
// return typeof(DateTime);
}
}

public override object DefaultNewRowValue
{
get
{
return "00000-000-00000-0";
}
}
}

public class MaskedTextBoxColumn : DataGridViewColumn
{
public MaskedTextBoxColumn()
: base(new MaskedTextBoxCell())
{
}

public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a
CalendarCell.
if (value != null &&
!
value.GetType().IsAssignableFrom(typeof(MaskedTextBoxCell)))
{
throw new InvalidCastException("Must be a
MaskedTextBoxCell");
}
base.CellTemplate = value;
}
}
}
class MaskedTextBoxEditingControl : MaskedTextBox,
IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;

public MaskedTextBoxEditingControl()
{
this.Mask = "00000-000-00000-0";
}

public string MaskFormat
{
set
{
this.Mask = "00000-000-00000-0";
}
}

// Implements the
IDataGridViewEditingControl.EditingControlFormattedValue
// property.
public object EditingControlFormattedValue
{
get
{
return this.Text;
}
set
{
if (value is String)
{
this.Text = (string)value;
}
}
}

// Implements the
// IDataGridViewEditingControl.GetEditingControlFormattedValue
method.
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}

// Implements the
// IDataGridViewEditingControl.ApplyCellStyleToEditingControl
method.
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;

//this.CalendarForeColor =
dataGridViewCellStyle.ForeColor;
//this.CalendarMonthBackground =
dataGridViewCellStyle.BackColor;
}

// Implements the
IDataGridViewEditingControl.EditingControlRowIndex
// property.
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}

// Implements the
IDataGridViewEditingControl.EditingControlWantsInputKey
// method.
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return false;
}
}

// Implements the
IDataGridViewEditingControl.PrepareEditingControlForEdit
// method.
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}

// Implements the IDataGridViewEditingControl
// .RepositionEditingControlOnValueChange property.
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}

// Implements the IDataGridViewEditingControl
// .EditingControlDataGridView property.
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}

// Implements the IDataGridViewEditingControl
// .EditingControlValueChanged property.
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}

// Implements the IDataGridViewEditingControl
// .EditingPanelCursor property.
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}

//protected override void OnValueChanged(EventArgs eventargs)
protected void OnValueChanged(EventArgs eventargs)
{
// Notify the DataGridView that the contents of the cell
// have changed.
valueChanged = true;

this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
//base.OnValueChanged(eventargs);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top