问题
My question might seems to be duplicate although it is not the same issue i had experienced before.
I have successfully added a richtexbox column to the datagridview as it has being my problem for a while. now you can add richtextbox to the datagridview.
Now am trying to highlight the edited text only but am not setting it work as after i edited the text it highlight the whole text.
for an example from what i wanna get
"Test" = Testing
from the example i above, i only want to highlight the only added one from the existing one.
the code below highlighted the whole text from the datagridview richtextbox cell which is not what i want.
code that am using for color change
private void Gridview_Output_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
Gridview_Output.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;
}
}
Richtextbox class
public partial class RichTextBoxControl : DataGridViewColumn
{
public RichTextBoxControl(): base()
{
base.CellTemplate = new RichTextboxCell1();
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (!((value == null)) && !(value.GetType().IsAssignableFrom(typeof(RichTextboxCell1))))
{
throw new InvalidCastException("Must be a CalendarCell");
}
base.CellTemplate = value;
}
}
}
public class RichTextboxCell1 : DataGridViewTextBoxCell
{
public RichTextboxCell1()
{
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
RichTextBoxEditingControl1 ctl = (RichTextBoxEditingControl1)DataGridView.EditingControl;
if (this.RowIndex >= 0)
{
if ((!object.ReferenceEquals(this.Value, DBNull.Value)))
{
if (this.Value != null)
{
if (this.Value != string.Empty)
{
try
{
ctl.Text =this.Value.ToString();
}
catch (Exception ex)
{
}
}
}
}
}
}
public override System.Type EditType
{
get
{
return typeof(RichTextBoxEditingControl1);
}
}
public override System.Type ValueType
{
get
{
return typeof(String);
}
}
public override object DefaultNewRowValue
{
get
{
return String.Empty;
}
}
}
class RichTextBoxEditingControl1 : RichTextBox, IDataGridViewEditingControl
{
private DataGridView dataGridViewControl;
private bool valueIsChanged = false;
private int rowIndexNum;
public RichTextBoxEditingControl1()
{
}
public object EditingControlFormattedValue
{
get
{
return this.Text;
}
set
{
if (value is string)
{
this.Text = value.ToString();
}
}
}
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return this.Text;
}
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}
public int EditingControlRowIndex
{
get
{
return rowIndexNum;
}
set
{
rowIndexNum = value;
}
}
public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
{
if (Keys.KeyCode == Keys.Left || Keys.KeyCode == Keys.Up || Keys.KeyCode == Keys.Down || Keys.KeyCode == Keys.Right || Keys.KeyCode == Keys.Home || Keys.KeyCode == Keys.End || Keys.KeyCode == Keys.PageDown || Keys.KeyCode == Keys.PageUp)
{
return true;
}
else
{
return false;
}
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
public DataGridView EditingControlDataGridView
{
get
{
return dataGridViewControl;
}
set
{
dataGridViewControl = value;
}
}
public bool EditingControlValueChanged
{
get
{
return valueIsChanged;
}
set
{
valueIsChanged = value;
}
}
public Cursor EditingControlCursor
{
get
{
return base.Cursor;
}
}
protected override void OnTextChanged(System.EventArgs eventargs)
{
valueIsChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnTextChanged(eventargs);
}
#region IDataGridViewEditingControl Members
Cursor IDataGridViewEditingControl.EditingPanelCursor
{
get { return base.Cursor; }
}
#endregion
}
来源:https://stackoverflow.com/questions/29629892/how-can-i-manage-changed-value-text-by-highlighting-the-edited-text-only