2011-10-08 14 views

答えて

19

DataGridViewCellFormattingイベントを処理し、フォントを太字のスタイルを適用する:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    var dataGridView = sender as DataGridView; 
    if (dataGridView.Rows[e.RowIndex].Selected) 
    { 
    e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold); 
    // edit: to change the background color: 
    e.CellStyle.SelectionBackColor = Color.Coral; 
    } 
} 
+0

助けてくれてありがとう!行のBackColorを変更する方法は? – Gali

+0

セル全体ではなく、セル内の一部の単語だけを太字で表示することはできますか?ハイライトを表示するためにこの機能が必要ですが、これを実装する方法はわかりません。まったく可能ですか? – FrenkyB

+1

@FrenkyB:コードプロジェクトの[RichTextBox Cell in a DataGridView](http://www.codeproject.com/Articles/31823/RichTextBox-Cell-in-a-DataGridView)の記事を参照してください。ただし、パフォーマンスの問題があるようです...あなたのデータが読まれるだけであればそれは受け入れられるかもしれません。 –

0

SelectionChangedデータグリッドビューのイベントを処理し、cellスタイルを設定してください。

1

内容をDatagridにロードした後、これらのイベントハンドラをRowEnterおよびRowLeaveに適用します。

private void dg_RowEnter(object sender, DataGridViewCellEventArgs e) 
{ 
    System.Windows.Forms.DataGridViewCellStyle boldStyle = new System.Windows.Forms.DataGridViewCellStyle(); 
    boldStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold); 
    dg.Rows[e.RowIndex].DefaultCellStyle = boldStyle; 
} 

private void dg_RowLeave(object sender, DataGridViewCellEventArgs e) 
{ 
    System.Windows.Forms.DataGridViewCellStyle norStyle = new System.Windows.Forms.DataGridViewCellStyle(); 
    norStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular); 
    dg.Rows[e.RowIndex].DefaultCellStyle = norStyle; 
} 

コードはテストされていません。しかし、うまくいくはずです。

希望します。セルが選択された行に属する場合

0

以下のコードは、選択された行の太字スタイルの下でフォントを行います。 "Total"は私のコードの最後の行のチェックです

protected void gvRow_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
    if (e.Row.Cells[rowIndex].Text == "Total") 
    { 
    e.Row.Font.Bold = true; 
    } 
} 
} 
関連する問題