[英]When i click another cell with different value, the back color become black in DataGridView textbox
我使用eventhandler在DataGridView中為textbox創建了一個代碼。
問題是當我單擊另一個具有不同值的單元格時,單元格的背面顏色變為黑色。
例如:我在DataGridView中有這些數據
1 1000
2000年2月
2000年3月
當我點擊1000時,我進展順利。 之后,我點擊2000,當前單元格的背面顏色為黑色。 但是,之后如果我點擊其他2000,背面顏色再次變白。
因此,如果突出顯示的單元格中的值發生更改,則會使背面顏色變為黑色。
有人可以幫我解決這個問題嗎?
這是文本框的代碼。
private void dgvSJ_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dgvSJ.CurrentCell.ColumnIndex == 10)
{
TextBox tx = e.Control as TextBox;
tx.TextChanged += new EventHandler(tx_TextChanging);
}
}
void tx_TextChanging(object sender, EventArgs e)
{
rowIndexCell = dgvSJ.CurrentRow.Index;
if (dgvSJ.Rows[rowIndexCell].Cells[10].EditedFormattedValue != null && dgvSJ.CurrentRow.Cells[10].EditedFormattedValue.ToString() != "")
{
dgvSJ.CurrentRow.Cells[10].Value = string.Format(GlobalVar.PriceFormat, Convert.ToDouble(dgvSJ.CurrentRow.Cells[10].EditedFormattedValue)); ![enter image description here][1]
}
}
使用與僅標准文本框相對的richTextBox,因為它允許您更改richTextBox的選擇顏色(而不是像上面那樣使用單元格。)
private void dgvSJ_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dgvSJ.CurrentCell.ColumnIndex == 10)
{
RichTextBox rtx = e.Control as RichTextBox ;
rtx.SelectionColor = Color.CornflowerBlue;
rtx.TextChanged += new EventHandler(tx_TextChanging);
}
}
您可以使用BackColor屬性更改文本控件的背景顏色以混合到窗體的顏色方案中。
XAML
<TextBox Height="23" HorizontalAlignment="Left" Margin="173,165,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Background="Red" />
C#
var tbox = new TextBox(){Background = color};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.