Thursday, June 2, 2011

Showing Multiline string in a DataGridView Cell

Recently I need to show multi-line string in a datagridview cell. To achieve that we need to write code in CellFormatting event of DataGridview. Following is the code

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.Value.Equals("Error"))
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.ForeColor = Color.White;

}
else
{
if ((!e.Value.Equals("OK")) && e.ColumnIndex==2)
{
e.CellStyle.BackColor = Color.Green;
e.CellStyle.ForeColor = Color.White;
e.CellStyle.WrapMode = DataGridViewTriState.True;
dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
}
}
}

No comments:

Post a Comment