Click here to Skip to main content
14,919,811 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.
I have a datagridview that has a number of datagridviewtextbox columns. When i start editing a cell its back color becomes black and nothing in the textbox is visible as its back color changes. I have set the backcolor of the current cell to white but it never changes the color. My code is

C#
sales_dataGridView.CurrentCell.Style.ForeColor = Color.Black;
sales_dataGridView.CurrentCell.Style.BackColor = Color.White;


How can i solve this problem?
Posted
Comments
Zasky 29-Sep-11 3:35am
   
Are you using a standard DataGridView, or a derived class you didn't write?
Are you handling the CellFormatting event and changing the CellStyle?
Is CellStyle modified anywhere else in your code?
shaijuvjohn 5-Oct-11 23:47pm
   
I am using a standard datagridview. The columns are not autogenerated. Each column is textbox column. I am having a textbox textchanged event handler for this cell and does not have any cellformating event handler.
kornakar 3-Oct-11 1:36am
   
You could put a breakpoint to the gridview's CellStyleChanged event to see when the color is changed.
kiran dangar 3-Oct-11 4:33am
   
Please provide more information on the issue
shaijuvjohn 5-Oct-11 23:47pm
   
I am using a standard datagridview. The columns are not autogenerated. Each column is textbox column. I am having a textbox textchanged event handler for this cell and does not have any cellformating event handler.
Zasky 6-Oct-11 5:56am
   
It might help if you post your TextChanged handler.
BobJanova 7-Oct-11 9:42am
   
Somewhere, you are setting the style and telling the grid to do this.

Test 1: create new, blank form, put a DataGridView on it, and see if it does the same thing. (It shouldn't.) If so, it is an app or OS level thing; try creating a new solution and making a form with a grid on.

After that, try searching your code for 'Style.' (without the quotes, matching case but not whole word), and see if there is somewhere (in designer code probably) where you set the style by mistake.

Attach handler on CellContentClick event and implenent this:

C#
void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
       {
           e.CellStyle.BackColor = Color.White;
       }


Best Regards!
   
Paste this code just above the column tag in source.

HTML
<SelectedRowStyle BackColor="#9FCACA" ForeColor="Black" />


i hope this will help you.


yogesh.
   
v2
Hello,

It seems that everything is ok with your code,when I use it it do exactly what it should do ,
but you have to check sales_dataGridView properties for DefaultCellStyle try:

C#
sales_dataGridView1.DefaultCellStyle.BackColor = Color.White;
sales_sales_dataGridView1.DefaultCellStyle.ForeColor = Color.Black;
sales_dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;
sales_dataGridView1.DefaultCellStyle.SelectionForeColor = Color.white;


This code should be typed in the program,
from where it has an influence on the whole program, in my case

C#
namespace Test_app
{
  /// <summary>
  /// Description of MainForm.
  /// </summary>
  public partial class MainForm : Form
  {
    public MainForm()
    {
	//
	// The InitializeComponent() call 
        //  is required for Windows Forms designer support.
	//
	InitializeComponent();
			
	//
	// TODO: Add constructor code after the InitializeComponent() call.
	//
	dataGridView1.DefaultCellStyle.BackColor = Color.White;
	dataGridView1.DefaultCellStyle.ForeColor = Color.Black;
	dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;
	dataGridView1.DefaultCellStyle.SelectionForeColor = Color.White;
        dataGridView1.Refresh();
      }
		
   void Button1Click(object sender, EventArgs e)
   .
   .// the rest of program
   .



After inserting the four lines of code, and if at no point throughout the program you did not have changed,


sales_dataGridView1.DefaultCellStyle.BackColor
sales_sales_dataGridView1.DefaultCellStyle.ForeColor
sales_dataGridView1.DefaultCellStyle.SelectionBackColor
sales_dataGridView1.DefaultCellStyle.SelectionForeColor
or
sales_dataGridView1.CellStyle.BackColor
sales_sales_dataGridView1.CellStyle.ForeColor
sales_dataGridView1.CellStyle.SelectionBackColor
sales_dataGridView1.CellStyle.SelectionForeColor


the result should be:
cell color after the selection of cell by clicking on it is red,
cell color after entering into it for editing content,by double clicking on it or by pressing enter is white,
text color is always black and the color of the cell that is not selected is white.

If the problem still occurs that DataGridView background color matches the color of the text when you enter the cell to modify the content, then you should check all the places in the program, for eventually inserted code for change of colors.
If the program is made ​​in Visual Tool, where you just insert DataGridView from the Tool panel onto the Mainform,
check the properties for DataGridView DefaultCellStyle ,and check the properties in EditColumns Dialog for DataGridView DefaultCellStyle.Sometimes the color settings in these two different options are different from the settings in the program code and can override simetimes the program code if you use Reset command or Refresh command somewhere in the program.
Also, the difference in settings of colors in properties for DataGridView DefaultCellStyle , and the properties in EditColumns Dialog for DataGridView DefaultCellStyle usually results with unresolved error in program,
which you don't see (program running is apparently normal ,but it have errors).
This applies to all settings in those two properties for DataGredView control.

All the best,
Peric Zeljko
periczeljkosmederevo@y ahoo.com
   
v2
Comments
shaijuvjohn 7-Oct-11 4:14am
   
Thanks for your reply. I tried the code. It worked fine. But the problem not solved. I think it happens when the cell contents are selected, that is when the textbox contents are selected.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900