想要使用变量(字符串变量)传递颜色名称 [英] Want to pass the color name using variable (string variable)

查看:145
本文介绍了想要使用变量(字符串变量)传递颜色名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想显示已售出"的记录Gridview中的库存车辆
使用出售的车辆等颜色,车辆的行颜色将为红色".库存中的颜色将为白色",我添加了一个颜色选择器c#项目,该代码为我提供了字符串中选定颜色的代码,例如红色".现在,我想使用字符串变量将"Red"之类的颜色字符串传递给我的Gridview CellFormatting代码.

请帮我如何将颜色选择器项目使用字符串变量传递给我的网格视图单元格格式代码的颜色字符串值.

网格视图单元格格式的代码为:-

Hi
I want to display the records of Sold & In-stock vehicals in Gridview
Using colors like for sold vehicals the row color will be "Red" for Vehical In stock Color will be "White" , I have added a color picker c# project that gives me the code of selected color in string like "Red" . Now i want to pass color strings like "Red" to my Gridview CellFormatting codes using a string variable .

Please help me how do I pass the color string value given by color picker project using a string variable to my Grid-view Cell Formatting codes .

codes for Grid-view Cell-formatting are :-

int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
if (rowIndex >= 0 && colIndex >= 0)
{
    DataGridViewRow theRow = dgvBillList.Rows[rowIndex];
    if (theRow.Cells[6].Value.ToString() == "Sold")
        theRow.DefaultCellStyle.BackColor = Color.Azure;
    try
    {
        if (rdbSell.Checked == true)
        {
            if (Convert.ToInt32(theRow.Cells[21].Value) != 0)
                theRow.DefaultCellStyle.BackColor = Color.Yellow;
        }
   }
    catch (Exception ex)
    {
    }

}


请帮帮我
提前谢谢.
拉吉


[edit]添加了代码块-OriginalGriff [/edit]


Please Help me
Thanks In advance .
Raj


[edit]Code block added - OriginalGriff[/edit]

推荐答案

我没有尝试过,但是您可以使用Color.Azure.ToString()将颜色转换为特定的字符串说.然后通过usin g Color.FromName()将其转换回特定的系统颜色.
请参阅 http://msdn.microsoft.com/en-us/library/system .drawing.color.fromname.aspx [ ^ ]以获得有关FromName的更多详细信息.
I have not tried this, but you could convert the color to the specific string by using Color.Azure.ToString() say. Then convert this back into a specific system color by using Color.FromName().
See http://msdn.microsoft.com/en-us/library/system.drawing.color.fromname.aspx[^] for more details on FromName.


Color类具有Name属性,该属性返回颜色的Name或十六进制值(如果它不是标准值).您可以通过FromName静态成员将标准值名称直接反馈回Color中:
The Color class has a Name property, which returns either the Name of the color or a hex value if it is not a standard value. You can feed the standard value names directly back into a Color via the FromName static member:
ColorDialog cd = new ColorDialog();
cd.ShowDialog();
Color c = cd.Color;
Console.WriteLine(c.ToString() + ":" + c.Name);
Color cc = Color.FromName(c.Name);
Console.WriteLine(cc.Name);
displayObject1.BackColor = c;
displayObject2.BackColor = cc;

请注意,如果您的原始颜色不是标准命名颜色,那么您将必须自己解析十六进制值,并使用FromArgb静态成员.

除非您自己剥离多余的元素,否则ToString对此将不起作用.

Note that if your original color is not a standard named color, then you will have to parse the hex value yourself, and use the FromArgb static member.

ToString will not work for this, unless you strip off the extraneous elements yourself.


这篇关于想要使用变量(字符串变量)传递颜色名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆