我的问题似乎可能重复,尽管这与我之前遇到过的问题不同。
我已成功将一个richtexbox列添加到datagridview,因为它已成为我的问题了一段时间。现在您可以将richtextbox添加到datagridview。
现在我正在尝试突出显示已编辑的文本,但我没有设置它,因为在我编辑文本后它突出显示整个文本。
来自我想要的一个例子
"试验" =测试 ing
从上面的示例i中,我只想突出显示现有的唯一一个。
下面的代码突出显示了datagridview richtextbox单元格中的全文,这不是我想要的。
用于换色的代码
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类
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
}