When I click another cell with a different value, the background color turns black in the DataGridView text box

I made code for a text field in a DataGridView with an event handler.

The problem is that I click on another cell with a different value, the background color of the cell turns black.

for example: I have this data in a DataGridView
1 1000
2 2000
3 2000

when i click on 1000 i went well. after that I press 2000, the back color for the current cell is black. But after that, if I click on another 2000, the back color will turn white again.

So, if the value in the selected cell is changed, this makes black black.

Can anyone help me solve this problem?

err This is the code for the text field.

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] } } 
+6
source share
2 answers

You can use the BackColor property to change the background color of a text element to fit the color scheme of your shapes.

Xaml

 <TextBox Height="23" HorizontalAlignment="Left" Margin="173,165,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Background="Red" /> 

FROM#

 var tbox = new TextBox(){Background = color}; 
0
source

Use a richTextBox opposite the standard text fields, as it allows you to change the color of the richTextBox selection (unlike a cell, as you do above).

 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); } } 
0
source

Source: https://habr.com/ru/post/907199/


All Articles