How do I apply colours to a string in a GridView?
private string ColouredString(string input)
{
//I would like to apply red c开发者_开发技巧olor to string 'nve'
string nve = "No Value";
return nve;
}
How do I apply a color to a string in a GridView?
String is a collection of characters in memory. It doesn't have any color information embedded in it. You need to set the color on the control that is displaying the string.
For example if it is a TextBox control it might have a ForeColor property or FontColor property, etc. Check the properties of the control/surface you're showing string on.
Add an event to your dataGridView
for dataGridView_CellValueChanged
:
private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
if (String.IsNullOrEmpty(dataGridView.CurrentCell.Value.ToString())) {
// Display error string in cell
dataGridView.CurrentCell.Value = "No Value";
// Set color to red
dataGridView.CurrentCell.Style.ForeColor = System.Drawing.Color.Red;
}
}
精彩评论