2010-12-15 17 views
11

データのバインドされたアイテムによって、各行の背景が異なるDataGridViewがあります。しかし、行を選択すると、元の背景色が見えなくなります。この問題を解決するにはDataGridViewの行:選択時の半透明の選択または行の境界

、私は2つの解決策を考えています

私は2つの選択行が異なる背景色を持っているかどうかを確認することが可能となる、選択は半透明にすることができます。

または、私は選択色を完全に削除し、選択した行の周りに境界線を描画することができます。

どのようなオプションが簡単ですか?どのようにすればいいですか?

これはWinFormアプリケーションです。

編集:私はあなたのコードの一部を使用して終了、漂流

private void dgv_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) 
    { 
     if (dgv.Rows[e.RowIndex].Selected) 
     { 
      var row = dgv.Rows[e.RowIndex]; 
      var bgColor = row.DefaultCellStyle.BackColor; 
      row.DefaultCellStyle.SelectionBackColor = Color.FromArgb(bgColor.R * 5/6, bgColor.G * 5/6, bgColor.B * 5/6); 
     } 
    } 

これは、半透明の選択色の印象を与えます。ご協力いただきありがとうございます!

答えて

7

選択した行を囲む枠線を描画する場合は、DataGridView.RowPostPaintEventを使用し、選択色を「クリア」するには、DataGridViewCellStyle.SelectionBackColorおよびDataGridViewCellStyle.SelectionForeColorプロパティを使用できます。例えば

、私はこの

row.DefaultCellStyle.BackColor = Color.LightBlue; 
row.DefaultCellStyle.SelectionBackColor = Color.LightBlue; 
row.DefaultCellStyle.SelectionForeColor = dataGridView1.ForeColor; 

ような行のセルスタイルを設定する場合、私はRowPostPaintEvent

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) 
{ 
    if (dataGridView1.Rows[e.RowIndex].Selected) 
    { 
     using (Pen pen = new Pen(Color.Red)) 
     { 
      int penWidth = 2; 

      pen.Width = penWidth; 

      int x = e.RowBounds.Left + (penWidth/2); 
      int y = e.RowBounds.Top + (penWidth/2); 
      int width = e.RowBounds.Width - penWidth; 
      int height = e.RowBounds.Height - penWidth; 

      e.Graphics.DrawRectangle(pen, x, y, width, height); 
     } 
    } 
} 

にこのコードを追加することができ、選択された行は次のように表示されます。

row with border

+0

私はこれにショットを付けました。それは問題ありませんでした。それから、新たな問題が浮かび上がってきました。透明な選択色は、並べ替えの後では本当に醜い(テキストは古いテキストやものになりました。))、別の解決策を探します。 –

+0

代わりに半透明の選択色を作成するためにコードを使用しました。編集を参照してください。ご協力いただきありがとうございます! –

関連する問題