基于文本c#windows应用程序窗体的Datagridview行颜色更改
你好, 我想根据列中的文本数据更改行颜色。假设我将文本设置为“ABUS_R/L”,我想将整行设置为绿色,将整行设置为黄色 我在for循环和if循环中尝试了以下语法:基于文本c#windows应用程序窗体的Datagridview行颜色更改,c#,asp.net,wpfdatagrid,C#,Asp.net,Wpfdatagrid,你好, 我想根据列中的文本数据更改行颜色。假设我将文本设置为“ABUS_R/L”,我想将整行设置为绿色,将整行设置为黄色 我在for循环和if循环中尝试了以下语法: DataRow row = table.NewRow(); // Insert New row in datagridview if (ByteArrayToHexString(buffer).StartsWith("AA") &&
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;
}
但是,它正在更改整个窗体的颜色,而不是仅更改行的颜色
有人能告诉我怎么做吗?您需要针对特定的
行,而不是整个DataGridView
dataGridView1.DefaultCellStyle.ForeColor = Color.Red;
我无法执行此操作。我没有为行获取DefaultCellStyle选项。这是因为您的行的类型为DataRow
,属于DataTable
,它没有颜色属性。您需要以DataGridView
的行为目标,但是如果没有与DataGridView
相关的任何代码,我们将无法提供帮助。我需要在DataGridView中处理此问题吗?不在数据表中?我将更新CodeYes—DataGridView
是Color属性存在的地方。DataGridView
是从DataTable
Ok获取和显示数据的控件。我将为这个问题添加新的线程并插入完整的代码。我无法在此处插入更改的代码
row.DefaultCellStyle.BackColor = Color.Red;