質問

私は、複数の行と列で構成されたDataGridViewのを持っています。 私はそれぞれの行を反復処理し、特定の列の内容を確認したいです。 その列が単語が含まれている場合は、「NO」、私は赤に行全体の前景色を変更したいです。 ここではいくつかのコードの試みは、これまでのところですが、それは確かに私はすべてのセルを反復処理する必要がある場合は疑問を開始し、動作していないのですか?

CODEます:

foreach (DataGridViewRow dgvr in dataGridView1.Rows)
        {
            if (dgvr.Cells["FollowedUp"].Value.ToString() == ("No"))
            {
                dgvr.DefaultCellStyle.ForeColor = Color.Red;
            }
        }
役に立ちましたか?

解決 3

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;
            }
        }
    }

他のヒント

フックアップの OnRowDataBound のイベント後、

ものを行います

ASPX(グリッド):

    <asp:.... OnRowDataBound="RowDataBound"..../>

背後にあるコードます:

    protected void RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex == -1)
        {
            return;
        }

        if(e.Row.Cells[YOUR_COLUMN_INDEX].Text=="NO"){
             e.Row.BackColor=Color.Red;   
        }
    }

リサイズFORます:

hook the **DataBindingComplete** event and do stuff in it:

     private void dataGridView1_DataBindingComplete(object sender, 
                       DataGridViewBindingCompleteEventArgs e)
    {
        if (e.ListChangedType != ListChangedType.ItemDeleted)
        {
            DataGridViewCellStyle red = dataGridView1.DefaultCellStyle.Clone();
            red.BackColor=Color.Red;

            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                if (r.Cells["FollowedUp"].Value.ToString()
                       .ToUpper().Contains("NO"))
                {
                    r.DefaultCellStyle = red;
                }
            }
        }
    }

あなたのDataGridViewで、CellFormattingイベントを処理ます:

dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);

あなたのイベントハンドラは、このようになります:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{       
    if(dataGridView1.Columns[e.ColumnIndex].Name == "FollowedUp" && e.Value != null && e.Value.ToString() == "No")
        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;  
}

このようにして、あなたが行にわたって「反復」されていません - 。単にグリッドで、彼らが見えるようになり(したがって、フォーマットする必要が)とき、彼らが描か/塗装されていると色を変える

これは、スペースや他の文字は、セルの値の一部として存在することは可能ですか?その場合はストレート平等ではなく、Containsメソッドを使用してみます。

if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("No"))

このはWinformsのためのソリューションです。

private void HighlightRows()
{
    DataGridViewCellStyle GreenStyle = null;

    if (this.dgridv.DataSource != null)
    {
        RedCellStyle = new DataGridViewCellStyle();
        RedCellStyle.BackColor = Color.Red;

        for (Int32 i = 0; i < this.dgridv.Rows.Count; i++)
        {
            if (((DataTable)this.dgridv.DataSource).Rows[i]["col_name"].ToString().ToUpper() == "NO")
            {
                this.dgridv.Rows[i].DefaultCellStyle = RedCellStyle;
                continue;
            }
        }
    }
}

このコードは、私のために正常に動作します:


foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if ((string)row.Cells["property_name"].Value == UNKNOWN_PROPERTY_NAME)
    {
        row.DefaultCellStyle.BackColor = Color.LightSalmon;
        row.DefaultCellStyle.SelectionBackColor = Color.Salmon;
    }
}

の文字列としてキャストではなく、ToStringメソッドを呼び出す以外に、私はそれが大文字と小文字の区別のバグかもしれないので、本当に何の違いを参照してくださいいけません。使用してみてください:

dgvr.Cells["FollowedUp"].Value.ToString().ToUpper() == "NO"
private void Grd_Cust_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    colorCode == 4 ? Color.Yellow : Color.Brown;
    if (e.RowIndex < 0 || Grd_Cust.Rows[e.RowIndex].Cells["FollowedUp"].Value == DBNull.Value)
        return;
    string colorCode = Grd_Cust.Rows[e.RowIndex].Cells["FollowedUp"].Value.ToString();
    e.CellStyle.BackColor = colorCode == "NO" ? Color.Red : Grd_Cust.DefaultCellStyle.BackColor;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top