单元格格式化数据
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 1 /*status列的Index*/)
{
if (object.Equals(e.Value, 0))
{
e.Value = "未完成";
e.CellStyle.ForeColor = Color.Red;
}
else
{
e.Value = "已完成";
e.CellStyle.ForeColor = Color.Green;
}
}
}
行数据格式化数据,一行的单元格数据可以根据另一个单元值而定
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
int rowsindex=e.RowIndex ;
int row_count=dataGridView1 .Rows.Count ;
if (rowsindex < row_count - 1)
{
DataGridViewRow dgr = dataGridView1.Rows[e.RowIndex];
try
{
dgr.Cells["name_values"].Value = dgr.Cells["Column0"].Value.ToString();
//dgr.DefaultCellStyle.ForeColor = 设置的颜色;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}