i have a datagridview in my C# program.after i bind it to a datasource , i want to replace some value in a specific column in automatically generated rows.for example i have columns in which have 0 or 1 values , and i want to replace true or false . what should i do ?
+1
A:
See the DataGridView.CellFormatting Event.
This event is called when it's time to format a cell. One of the more useful things you can do in this event is change the content and appearance of a cell based on the cell's value.
You'll want to study the code in the documentation but I have a snippet below that shows a little you can do. The CellFormatting event below checks if it's formatting a cell in a column called "Column3" and if it is and the cell's value is 0 (as a string) ,it changes the cell's value to "False" and the cell's BackColor to red.
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
if (dataGridView1.Columns[e.ColumnIndex].Name == "Column3") {
if (e.Value.ToString() == "0") {
e.CellStyle.BackColor = Color.Red;
e.Value = "False";
}
}
}
Jay Riggs
2009-11-11 18:30:21