Custom DataGridViewColumn. Doesn't accept some of the chars. Help

L

lbelkova

Hello,

I've created a custom DataGridViewColumn. Everything work well, except
for some reason the column doesn't accept some of the chars: "q", "."
and "'". Did anybody have a similar problem?
Please let me know. Any help will be gratefully appreciated.

Thanks.

my code:

public class RichTextBoxColumn : DataGridViewColumn
{
public RichTextBoxColumn() : base(new RichTextBoxCell())
{
}

public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a
RichtextboxCell.
if (value != null &&
!
value.GetType().IsAssignableFrom(typeof(RichTextBoxCell)))
{
throw new InvalidCastException("Must be a
RichTextBoxCell");
}
base.CellTemplate = value;
}
}
}

public class RichTextBoxCell : DataGridViewTextBoxCell
{
public RichTextBoxCell()
: base()
{
//None
}

public override void InitializeEditingControl(int rowIndex,
object
initialFormattedValue, DataGridViewCellStyle
dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell
value.
base.InitializeEditingControl(rowIndex,
initialFormattedValue,
dataGridViewCellStyle);
RichTextBoxEditingControl ctl = DataGridView.EditingControl as
RichTextBoxEditingControl;
if (this.Value==null)
ctl.Text =string.Empty;
else
ctl.Text = (string)this.Value;
}

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

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

public override object DefaultNewRowValue
{
get
{
// Use empty string as the default value.
return string.Empty;
}
}
}

class RichTextBoxEditingControl : RichTextBox,
IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;

public RichTextBoxEditingControl()
{
//None;
}

// 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.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = 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 RTB 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 OnTextChanged(EventArgs eventargs)
{
// Notify the DataGridView that the contents of the cell
// have changed.
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnTextChanged(eventargs);
}
}


here is how i add the column to my gridview:

RichTextBoxColumn rtbcol = new RichTextBoxColumn();
rtbcol.HeaderText = "my_caption";
rtbcol.DataPropertyName = "my_field"
rtbcol.Width = 250;
rtbcol.ReadOnly = true;
rtbcol.DefaultCellStyle.Alignment =
DataGridViewContentAlignment.TopLeft;
GrdTestSteps.Columns.Add(rtbcol);
 

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