如何更改datagridiew最后一行C#的字体颜色 [英] How to change font color of datagridiew last row C#
问题描述
大家好!我想知道如何在最后一行更改datagridview的forecolor。
我尝试过:
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。行[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想要改变sun和sum1 forecolor in red
你可以直接设置(整个)Row BackColor或ForeColor:YourRow.DefaultCellStyle.ForeColor = Color.Red;您可以通过以下方式设置特定Cell的颜色:
YourCell.Style.ForeColor = Color.Red;StackOverFlow上的这个线程包含许多使用DataGridView Color样式的有价值的例子;我建议你先阅读这篇文章,其中展示了如何使用DataGridVew的RowPrePaint事件:[ ^ ]。
并且,请记住,您可以创建要使用的预定义样式:DataGridViewCellStyle YourRedCellStyle = new DataGridViewCellStyle() ;
YourRedCellStyle.ForeColor = Color.Red;
//正在使用中:
YourCell.CellStyle = YourRedCellStyle;
尝试
if (dataGridView1.Rows.Count > 0 )
dataGridView1.Rows [dataGridView1.Rows.Count-1] .DefaultCellStyle.BackColor = Color.Red;
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
You can set the (entire) Row BackColor or ForeColor directly:YourRow.DefaultCellStyle.ForeColor = Color.Red;You can set a particular Cell's colors by:
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;
try
if (dataGridView1.Rows.Count > 0) dataGridView1.Rows[dataGridView1.Rows.Count-1].DefaultCellStyle.BackColor = Color.Red;
这篇关于如何更改datagridiew最后一行C#的字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!