Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我单击另一个具有不同值的单元格时,DataGridView文本框中的背景色变为黑色_C#_Winforms_Visual Studio 2008_Datagridview_Textbox - Fatal编程技术网

C# 当我单击另一个具有不同值的单元格时,DataGridView文本框中的背景色变为黑色

C# 当我单击另一个具有不同值的单元格时,DataGridView文本框中的背景色变为黑色,c#,winforms,visual-studio-2008,datagridview,textbox,C#,Winforms,Visual Studio 2008,Datagridview,Textbox,我用eventhandler在DataGridView中为textbox编写了一个代码 问题是,当我单击另一个具有不同值的单元格时,该单元格的背景色变为黑色 例如:我在DataGridView中有这些数据 1 1000 2000年 3 2000 当我点击1000时,我进展顺利。之后,我单击2000,当前单元格的背景色为黑色。但是,在那之后,如果我点击另一个2000,背景色又变白了 因此,如果高亮显示单元格中的值发生更改,则会使背景色变为黑色 有人能帮我解决这个问题吗 这是文本框的代码

我用eventhandler在DataGridView中为textbox编写了一个代码

问题是,当我单击另一个具有不同值的单元格时,该单元格的背景色变为黑色

例如:我在DataGridView中有这些数据
1 1000
2000年
3 2000

当我点击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]            

        }
    }

您可以使用BackColor属性更改文本控件的背景色,以融入表单的配色方案

XAML


使用richTextBox而不仅仅是标准文本框,因为它允许您更改richTextBox的选择颜色(与上面的单元格相反)


您是在谈论选择/突出显示颜色吗?在你的外观控制面板中改变它…我已经用另一种不是黑色的颜色设置了它。但它总是变为黑色,这不是WPF的问题。WinForms不使用XML。
<TextBox Height="23" HorizontalAlignment="Left" Margin="173,165,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Background="Red" />
var tbox = new TextBox(){Background = color};
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);
    }
}