我创建了一个DataGridView,其列类型为RichTextBox控件。使用此处的代码RichTextBox Cell in a DataGridView
但是当我编辑DataGridViewCell时,如下所示。它不按我想要的方式工作。
//Adding the datagridview column and two rows.
DataGridViewRichTextBoxColumn richTextColumn = new DataGridViewRichTextBoxColumn();
richtextcolumn.Name = "blah";
this.dataGridView1.Columns.Add(richTextColumn);
this.dataGridView1.Rows.Add("one two three four");
this.dataGridView1.Rows.Add("five six seven eight");
//Edit cell
DataGridViewCell cell = row.Cells["blah"];
dataGridView1.CurrentCell = cell;
dataGridView1.BeginEdit(true);
RichTextBox temp = dataGridView1.EditingControl as RichTextBox;
temp.SelectionStart = 3;
temp.SelectionLength = 4;
temp.SelectionBackColor = Color.Red;
dataGridView1.EndEdit();
现在这正确地改变了我选择的背景颜色,但是一旦我离开细胞。然后它会更改以显示所有rtf信息。比如这个
{\ rtf1 \ ansi \ ansicpg1252 \ deff0 \ deflang3081 {\ fonttbl {\ f0 \ fnil \ fcharset0 Microsoft Sans Serif;}} {\ colortbl; \ red255 \ green0 \ blue0;} \ viewkind4 \ uc1 \ pard \ f0 \ fs17五\ highlight1六\ highlight0七八\ par }
在我看来,在单元格编辑完成后,它不再托管RichTextBox控件。我如何坚持这些变化。