簡體   English   中英

基於Text C#Windows應用程序窗體的Datagridview行顏色更改

[英]Datagridview Row Color change based on Text c# windows application form

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 Loop和if Loop中嘗試了以下語法:

dataGridView1.DefaultCellStyle.ForeColor = Color.Red;

但是,它正在更改整個表單顏色,而不是僅更改行顏色。

那么有人可以告訴我我該怎么做嗎?

您需要定位特定的row ,而不是整個DataGridView

row.DefaultCellStyle.BackColor = Color.Red; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM