问题描述:

I am working in C# windows form application and I want to change color of string which I am displaying in a grid.

//dtRList is a DataTable

dtRList.Rows.Add(something + "This should be red");

Basically I need to change the color of the string that I am appending here. Something like this,

String str = "This should be red";

now change the color of str to Red and

dtReconList.Rows.Add(something + str);

I do not think this is possible to do like this, may be i will have to go for a label or something.

Any help is appreciated.

Thanks.

网友答案:

You can not change to color of a string. Because it has no color to begin with, what you want, as you said, is to change the color of the component which represents your string value.

One quick code to give you a general idea, here's how you would set the color of a DataGridViewCell

DataGridViewCellStyle style = new DataGridViewCellStyle();
style.ForeColor = Color.Red;

dgVenta.Rows[0].Cells[0].Style = style;

Of course you need to style the cell you want and not necessarily the first one. That's up to you. Keep in mind that if the row you changed is selected it may not reflect the color you set, to solve that look the SelectionForeColor property.

With all that you can, of course, change everything you need and do whatever conditional logic such as use green or red color if the value is positive or negative, for example.

网友答案:

DataTable is not related to UI. It is not a control. It's just a data holder. Question would have sense if you would ask about DataGridView control or some other way of displaying data. But string is a sequence of characters. String holds text and nothing more. String has no knowledge of the way you will use it (e.g. display string).

相关阅读:
Top