Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用criterea交替着色DataGridView行,同时保持着色单元格不变_C#_Linq_Datagridview_Foreach_Casting - Fatal编程技术网

C# 使用criterea交替着色DataGridView行,同时保持着色单元格不变

C# 使用criterea交替着色DataGridView行,同时保持着色单元格不变,c#,linq,datagridview,foreach,casting,C#,Linq,Datagridview,Foreach,Casting,我确实有一个问题,但我在输入问题时解决了它。我希望没有人介意我仍然把它贴在这里,因为我想我可能会对其他人有所帮助,因为我在stackoverflow上找不到完全像这样的东西。这是我的第一篇帖子,所以如果我不应该那么做,请告诉我;) 我正在按示例为DataGridView中的行着色。但我有一些细胞已经着色了,我想保持这种着色 var sampleRows = dgvData.Rows.Cast<DataGridViewRow>() .Where(x=>x.

我确实有一个问题,但我在输入问题时解决了它。我希望没有人介意我仍然把它贴在这里,因为我想我可能会对其他人有所帮助,因为我在stackoverflow上找不到完全像这样的东西。这是我的第一篇帖子,所以如果我不应该那么做,请告诉我;)

我正在按示例为DataGridView中的行着色。但我有一些细胞已经着色了,我想保持这种着色

    var sampleRows = dgvData.Rows.Cast<DataGridViewRow>()
        .Where(x=>x.Cells["Sample"].Value.ToString().Substring(0,4) == "SAMP");

    string lastSample = "";
    Color backColor = Color.LightGray;
    foreach (DataGridViewRow row in sampleRows) {
        string currentSample = row.Cells["Sample"].Value.ToString().Substring(0,15);
        if (currentSample != lastSample)
        {
            backColor = (backColor == Color.LightGray) ? Color.DarkGray : Color.LightGray;
            lastSample = currentSample;
        } 
        row.Cells.Cast<DataGridViewCell>()
            .Where(x => x.Style.BackColor == default(Color))
            .ToList().ForEach(x => x.Style.BackColor = backColor);
    }
var sampleRows=dgvData.Rows.Cast()
其中(x=>x.Cells[“Sample”].Value.ToString().Substring(0,4)=“SAMP”);
字符串lastSample=“”;
颜色背景色=Color.LightGray;
foreach(采样器OWS中的DataGridViewRow行){
字符串currentSample=row.Cells[“Sample”].Value.ToString().Substring(0,15);
如果(currentSample!=lastSample)
{
backColor=(backColor==Color.LightGray)?Color.DarkGray:Color.LightGray;
lastSample=currentSample;
} 
row.Cells.Cast()
。其中(x=>x.Style.BackColor==默认值(颜色))
.ToList().ForEach(x=>x.Style.BackColor=BackColor);
}

我们仍然感谢您的反馈。

我投票决定将此问题作为离题题来结束,因为这不是一个问题。很高兴您能解决它。不谈主题。谢谢,下次我会的。