【问题标题】:Datagridview Row Color change based on Text c# windows application form基于Text c# windows应用表单的Datagridview Row颜色变化
【发布时间】:2017-05-31 10:47:47
【问题描述】:
DataRow row = table.NewRow();                                           // Insert New row in datagridview

    if (ByteArrayToHexString(buffer).StartsWith("AA") && ByteArrayToHexString(buffer).Substring(6, 2).StartsWith("11"))
    {

        table.Rows.Add(row);                                                // Add new row
        row["Source"] = string.Format("{0}", "ABUS_L/R");                   // Insert source name in row
        row["System Time"] = DateTime.Now.ToString("HH:mm:ss:fff");         // Insert time in row
                                                                          //row["Source"] = ByteArrayToHexString(buffer).Substring(9, 2);     
        row["T.S Begin"] = ByteArrayToHexString(buffer).Substring(8, 8);    // Insert TS Begin
        row["T.S End"] = ByteArrayToHexString(buffer).Substring(16, 8);    // Insert TS End
        row["Length"] = ByteArrayToHexString(buffer).Substring(24, 2);      // Insert length Field
        row["Data"] = ByteArrayToHexString(buffer).Substring(26, 2);        // Insert Data field
        row["CRC"] = ByteArrayToHexString(buffer).Substring(28, 2);         // Compare CRC field with Protocol & validate the information
        row["CRC_PC"] = crc8.ComputeChecksum(buffer);                       // Compute & compare CRC generated by PC                                                                                                                // Add new row
        //dataGridView1.Update();                                           // Update datagridview
    return;

    }

    //Case for ABUS_RK

    if (ByteArrayToHexString(buffer).StartsWith("AA") && ByteArrayToHexString(buffer).Substring(6, 2).StartsWith("12"))
    {
        table.Rows.Add(row);                                                // Add new row

        row["Source"] = string.Format("{0}", "ABUS_RK");
        row["System Time"] = DateTime.Now.ToString("HH:mm:ss:fff");
    //row["Source"] = ByteArrayToHexString(buffer).Substring(9, 2);

        row["T.S Begin"] = ByteArrayToHexString(buffer).Substring(8, 8);
        row["T.S End"] = ByteArrayToHexString(buffer).Substring(16, 8);
        row["Length"] = ByteArrayToHexString(buffer).Substring(24, 2);
        row["Data"] = ByteArrayToHexString(buffer).Substring(26, 2);
        row["CRC"] = ByteArrayToHexString(buffer).Substring(28, 2);
        row["CRC_PC"] = crc8.ComputeChecksum(buffer);
        //table.Rows.Add(row);
        //dataGridView1.Update();
    return;

    }

你好,

我想根据列中的文本数据更改行颜色。假设我的文本为“ABUS_R/L”,我想将整行设为绿色,将“ABUS_RK”整行设为黄色。

我在 for 循环和 if 循环中尝试了以下语法:

dataGridView1.DefaultCellStyle.ForeColor = Color.Red;

然而,它改变了整个表单的颜色,而不是仅仅改变了行的颜色。

那么谁能告诉我我该怎么做呢?

【问题讨论】:

    标签: c# asp.net wpfdatagrid


    【解决方案1】:

    您需要针对特定​​的row,而不是整个DataGridView

    row.DefaultCellStyle.BackColor = Color.Red; 
    

    【讨论】:

    • 我无法执行此操作。我没有得到行的 DefaultCellStyle 选项。
    • 那是因为你的行属于DataRow 类型,属于DataTable 这没有颜色属性。您需要定位 DataGridView 的行,但是如果没有与 DataGridView 相关的任何代码,我们将无法提供帮助
    • 我需要在 Datagridview 中处理这个吗?不在数据表中?我会更新代码
    • 是 - DataGridView 是 color 属性所在的位置。 DataGridView 是从 DataTable 获取和显示数据的控件
    • 好的。我将为这个问题添加新线程并插入完整的代码。我无法在此处插入更改的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    相关资源
    最近更新 更多