C#迭代DataGridView

C#迭代DataGridView

本文介绍了C#迭代DataGridView&更改行颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview由多行和列组成。
我想遍历每一行并检查特定列的内容。
如果该列包含单词否,我想将整行的前缀更改为红色。
这是到目前为止的一些代码尝试,但它肯定不起作用,开始想知道如果我需要迭代每个单元格?



代码: p>

  foreach(dataGridView1.Rows中的DataGridViewRow dgvr)
{
if(dgvr.Cells [FollowedUp] .Value.ToString()==(否))
{
dgvr.DefaultCellStyle.ForeColor = Color.Red;
}
}


解决方案
  public void ColourChange()
{
DataGridViewCellStyle RedCellStyle = null;
RedCellStyle = new DataGridViewCellStyle();
RedCellStyle.ForeColor = Color.Red;
DataGridViewCellStyle GreenCellStyle = null;
GreenCellStyle = new DataGridViewCellStyle();
GreenCellStyle.ForeColor = Color.Green;


foreach(DataGridViewRow dgvr in dataGridView1.Rows)
{
if(dgvr.Cells [FollowedUp]。Value.ToString()。包含(否))
{
dgvr.DefaultCellStyle = RedCellStyle;
}
if(dgvr.Cells [FollowedUp]。Value.ToString()。Contains(Yes))
{
dgvr.DefaultCellStyle = GreenCellStyle;
}
}
}


I have a datagridview made up of multiple rows and columns.I want to iterate through each row and check the contents of a specific column.If that column contains the word "NO", I want to change the forecolor of the entire row to Red.Here is an attempt at some code so far but It's certainly not working, starting to wonder If I need to iterate over every cell?

CODE:

foreach (DataGridViewRow dgvr in dataGridView1.Rows)
        {
            if (dgvr.Cells["FollowedUp"].Value.ToString() == ("No"))
            {
                dgvr.DefaultCellStyle.ForeColor = Color.Red;
            }
        }
解决方案
public void ColourChange()
    {
        DataGridViewCellStyle RedCellStyle = null;
        RedCellStyle = new DataGridViewCellStyle();
        RedCellStyle.ForeColor = Color.Red;
        DataGridViewCellStyle GreenCellStyle = null;
        GreenCellStyle = new DataGridViewCellStyle();
        GreenCellStyle.ForeColor = Color.Green;


        foreach (DataGridViewRow dgvr in dataGridView1.Rows)
        {
            if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("No"))
            {
                dgvr.DefaultCellStyle = RedCellStyle;
            }
            if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("Yes"))
            {
                dgvr.DefaultCellStyle = GreenCellStyle;
            }
        }
    }

这篇关于C#迭代DataGridView&更改行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:17