开发者

How do I apply colours to a string in a GridView?

开发者 https://www.devze.com 2023-04-04 00:05 出处:网络
private string ColouredString(string input) { //I would like to apply red c开发者_开发技巧olor to string \'nve\'
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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消