Click here to Skip to main content
15,908,901 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,
I want change cell color and style while entering data into it.
I used following code but not work. Please help me.
dataGridView1.Rows.Add(strAccountName).DefaultCellStyle.BackColor =Color.Yellow;
Please note that I want to change fore color and boldness.

What I have tried:

I tried following code but not working. Please siggest

dataGridView1.Rows.Add(strAccountName).DefaultCellStyle.BackColor =Color.Yellow;
Posted
Updated 27-Jul-16 2:56am

use CellValueChanged and EditingControlShowing event, customize it depends on your need.

C#
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
       {
           var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
           cell.Style = new DataGridViewCellStyle() { BackColor = Color.White };
           string fontName = "Microsoft Sans Serif";
           cell.Style.Font = new Font(fontName, 8.25f, FontStyle.Regular, GraphicsUnit.Pixel);

       }



       private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
       {
           e.CellStyle.BackColor = Color.Yellow;
           e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);

       }
 
Share this answer
 
v2
Comments
Ashraf680 28-Jul-16 1:08am    
Sir,
I wand to add data to DataGridView with different color and style while clicking a button.Please note that changing color and style must happen when the data entering to the DataGridview. And this want to be dynamically.
Karthik_Mahalingam 28-Jul-16 2:18am    
Hi Ashraf
these things you havent mentioned in the question..
i have given the solution for what you have asked.

provide more input clearly, i shall provide you better solution.
Ashraf680 28-Jul-16 4:21am    
Thank you sir...
Ashraf680 28-Jul-16 4:28am    
Please note that I want o generate a report in DataGridView as by taking data from two table. I created a sale Invoice. Main details (Party Name, Sale Invoice Number ..etc) storing in one table and Product details(Sale invoice Id,Product ID, Quantity, Rate...etc) storing in another table. While generating report, I have to take Party name from one table(I want to show this in color change with bold) and product details taking from second table(want to add in normal font and color).
Karthik_Mahalingam 28-Jul-16 4:33am    
report or gridview..
report means we are going out of the topic. i would encourage you to post a new question.
Hi try this

private DataGridViewTextBoxEditingControl withEventsField_EditControl;
private DataGridViewTextBoxEditingControl EditControl {
get { return withEventsField_EditControl; }
set {
if (withEventsField_EditControl != null) {
withEventsField_EditControl.KeyDown -= EditControl_KeyDown;
}
withEventsField_EditControl = value;
if (withEventsField_EditControl != null) {
withEventsField_EditControl.KeyDown += EditControl_KeyDown;
}
}
}

private void DGV_EditingControlShowing(object sender, System.Windows.Forms.DataGridViewEditingControlShowingEventArgs e)
{
if (EditControl == null) {
EditControl = (DataGridViewTextBoxEditingControl)e.Control;
}
}
private void EditControl_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
e.CellStyle.ForeColor = Color.Red;
e.CellStyle.BackColor = Color.Black;
e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);
}
 
Share this answer
 
Comments
Ashraf680 28-Jul-16 1:07am    
Sir,
I wand to add data to DataGridView with different color and style while clicking a button.Please note that changing color and style must happen when the data entering to the DataGridview. And this want to be dynamically.
vatsa_gaurav 28-Jul-16 7:07am    
Then its better to use Rowadded even of datagridview.
Try following code, it would help you achieve the thing.
this.dataGridView1.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this.dataGridView1_RowsAdded);
//in code:
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
if (cheque == true)
{
dataGridView1.Rows[e.RowIndex].Cells[2].Style.ForeColor = Color.Red;
}
else
{
dataGridView1.Rows[e.RowIndex].Cells[2].Style.ForeColor = Color.Green;
}
}
Ashraf680 28-Jul-16 8:01am    
I have some hope in this code....thank you..

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