简体   繁体   中英

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;

    }

Hello,

I want to Change the row Color based on the text data in the column. Suppose I have text as "ABUS_R/L" I want to make whole row Green, for "ABUS_RK" whole row as yellow.

I tried the following Syntax in for Loop & if Loop:

dataGridView1.DefaultCellStyle.ForeColor = Color.Red;

However, it is changing the whole form color instead of only changing the row color.

So can anybody tell me how I can do same?

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

row.DefaultCellStyle.BackColor = Color.Red; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM