5

フォント サイズとスタイルを変更する必要がある DataGridView (WinForm アプリケーション) の列があります。ここの記事から: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.font.aspx、私は以下のコードが私が望む結果を得ると考えています(私は最初にスタイリングを変更してテストします):

this.dataGridViewMain.Columns[3].DefaultCellStyle.Font = new Font(dataGridViewMain.DefaultCellStyle.Font, FontStyle.Italic);

しかし、コードは何も変更しません。また、イベント ハンドラーにコードを追加しようとしましたRowPostPaintが、まだ機能しません。プログラムで使用されるフォントがプロパティに設定されていることは知っていDataGridView.RowsDefaultCellStyleますが、イベントにコードを配置するRowPostPaintとそれが上書きされると思いました。以下は、RowPostPaintイベントのコードです。

void dataGridViewMain_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    this.dataGridViewMain.Columns[3].DefaultCellStyle.BackColor = Color.Gray;
    foreach (DataGridViewRow row in this.dataGridViewMain.Rows)
    {
        int daysInShop = Convert.ToInt32(row.Cells["Days in the shop"].Value);
        if (daysInShop > 4)
        {
            row.DefaultCellStyle.BackColor = Color.Red;
            row.DefaultCellStyle.ForeColor = Color.White;
        }
        else if (daysInShop > 2)
        {
            row.DefaultCellStyle.BackColor = Color.Yellow;
        }
        else
        {
            row.DefaultCellStyle.BackColor = Color.YellowGreen;
        }
        row.Height = 35;
    }

    this.dataGridViewMain.CurrentCell = null; // no row is selected when DGV is displayed
}

どんな助けでも大歓迎です。ありがとう。

4

4 に答える 4

7

これは私が見つけたものです。その下InitializeComponent()に次の行があります。

dataGridViewCellStyle3.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

その行をコメントアウトすると、列を斜体にするコードが正常にRowPostPaint機能します。RowPostPaint次に、他の列のフォントが太字でサイズが小さくなるように、以下のコードを追加しました。DataGridView.Columns[colNumber].DefaultCellStyle.Fontなぜオーバーライドしないのか、まだよくわかりませんdataGridViewCellStyle3

 int colCount = dataGridViewMain.ColumnCount;

 for (int i = 0; i < colCount; i++)
 {
     if(i != 3)
         this.dataGridViewMain.Columns[i].DefaultCellStyle.Font = new System.Drawing.Font("Verdana", 14F, FontStyle.Bold);
     else
         this.dataGridViewMain.Columns[3].DefaultCellStyle.Font = new System.Drawing.Font("Verdana", 25F, FontStyle.Bold);
 }
于 2013-03-15T17:08:06.673 に答える
4

フォント サイズは読み取り専用なので、新しいフォントを作成し、yourDataGridView.Font = new Font(name,size,style) を設定する必要があります。詳細については、http://msdn.microsoft.com/en-us/library/ を参照してください。 system.drawing.font.aspx

于 2013-03-15T13:34:04.043 に答える
-1

after に設定RowsDefaultCellStyleします。nullInitializeComponent()

DataGridViewグリッド/列/行の順番でスタイルをとっていると思います。したがって、行スタイルが設定されている場合、常に列スタイルがオーバーライドされます。

私の見解では、これは貧弱なデザインです。デフォルトの行スタイルはまったく必要ありません!

于 2018-04-15T07:40:01.460 に答える