Click here to Skip to main content
15,885,669 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello guys! i want to know how to change forecolor of datagridview last row.

What I have tried:

textBox2.Text = Add_Food_Inventory.setaluefortext204;
dateTimePicker1.Text = System.DateTime.Now.ToString();
dateTimePicker2.Text = Add_Food_Inventory.setaluefortext202;
dateTimePicker3.Text = Add_Food_Inventory.setaluefortext203;
button1.PerformClick();
int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
}
textBox1.Text = sum.ToString();
int sum1 = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum1 += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
}
textBox3.Text = sum1.ToString();
//PopulateRows();
int rowIdx = dataGridView1.Rows.Count - 1;
dataGridView1.Rows[rowIdx].Cells[0].Value = "Total";
dataGridView1.Rows[rowIdx].Cells[1].Value = sum;
dataGridView1.Rows[rowIdx].Cells[2].Value = sum1;
i want to change sun and sum1 forecolor in red
Posted
Updated 4-Sep-16 23:22pm

You can set the (entire) Row BackColor or ForeColor directly:
C#
YourRow.DefaultCellStyle.ForeColor = Color.Red;
You can set a particular Cell's colors by:
C#
YourCell.Style.ForeColor = Color.Red;
This thread on StackOverFlow contains a number of valuable examples of working with DataGridView Color styles; I suggest you start by reading this post which shows how to use the DataGridVew's RowPrePaint Event: [^].

And, remember that you can create pre-defined Styles to use:
DataGridViewCellStyle YourRedCellStyle = new DataGridViewCellStyle();
YourRedCellStyle.ForeColor = Color.Red;

// in use:
YourCell.CellStyle = YourRedCellStyle;
 
Share this answer
 
try

C#
if (dataGridView1.Rows.Count > 0)
           dataGridView1.Rows[dataGridView1.Rows.Count-1].DefaultCellStyle.BackColor = Color.Red;
 
Share this answer
 

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