0

DataGridViewフォームとコントロールに基づくアプリケーションを作成しています。

私はデータベースから情報をバインドしています。今やろうとしているのは、値に応じて列プロパティのフォントスタイルと色を変更すること"Urgent","Haute","Normale"です。

これが私が使用しているコードですが、機能しませんでした。誰かが以下のコードの何が問題になっているのか教えてもらえますか?

コード:

private void ColorizeCellsbyValue() {

        DataGridViewCellStyle BoldRed = null;
        BoldRed = new DataGridViewCellStyle();
        BoldRed.Font = new Font("Tahoma", 9, FontStyle.Bold);
        BoldRed.ForeColor = Color.Red;

        DataGridViewCellStyle Red = null;
        Red = new DataGridViewCellStyle();
        Red.ForeColor = Color.Red;

        DataGridViewCellStyle Black = null;
        Black = new DataGridViewCellStyle();
        Black.ForeColor = Color.Black;
        string priority;
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            priority = row.Cells[3].Value.ToString();
            switch (priority)
            {
                //Change font 
                case "Urgent":
                    row.Cells[3].Style = BoldRed;
                    break;
                case "Haute":
                    row.Cells[3].Style = Red;
                    break;
                case "Normale":
                    row.Cells[3].Style = Black;
                    break;
                default:
                    row.Cells[3].Style = Black;
                    break;
            }
        }
}
4

2 に答える 2

1

プロパティDataGridViewCellStyleを設計するために を作成する必要はありません。Column私の簡単な例を理解しようとする

        foreach (DataGridViewRow rows in dataGridView1.Rows)
        {
            if (rows.Cells[3].RowIndex % 2 == 0)
            {
                rows.Cells[3].Style.Font = new Font("Tahoma", 9, FontStyle.Bold);
                rows.Cells[3].Style.BackColor = Color.Red;
            }
            else
            {
                rows.Cells[3].Style.Font = new Font("Arial", 9, FontStyle.Regular);
                rows.Cells[3].Style.BackColor = Color.Blue;
            }
        }

あなたの主な問題に対する私の答えは、使用しようとすることです.EditedFormattedValue.ToString()

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            priority = row.Cells[3].EditedFormattedValue.ToString();
            switch (priority)
            {
                //Change font 
                case "Urgent":
                    row.Cells[3].Style = BoldRed;
                    break;
                case "Haute":
                    row.Cells[3].Style = Red;
                    break;
                case "Normale":
                    row.Cells[3].Style = Black;
                    break;
                default:
                    row.Cells[3].Style = Black;
                    break;
            }
        }
于 2013-02-08T12:18:59.830 に答える
1

使ってみて

row.Cells[3].Style.BackColor = <Color>;

使用ForeColorする

row.Cells[3].Style.ForeColor = <Color>;

これはうまくいくはずです。ハッピーコーディング!

于 2013-02-08T11:53:20.453 に答える