PC Review


Reply
Thread Tools Rate Thread

DataGridView - Change selection color when focus lost

 
 
Steve K
Guest
Posts: n/a
 
      30th Dec 2008
(I had posted this in the C# group and then found this group which I think is more appropriate)

I realized tonight that the DataGridView doesn't support a different selection color when it doesn't have focus. After
realizing this I set out on what I thought would be a quick and simple task to solve the problem.

Granted I'm no DataGridView expert, but to me this seems like it should work and it's not. I've tried adding explicit
calls to Invalidate() and it makes no different.

The code below is attempting to set the selection background color to Red when blurred and Blue when focused. These are
of course just testing colors.

I would really appreciate any tips any of you may have.

Note: I left the commented code in place so that you can see the original approach that I took before reducing this to a
dead-simple test.



[code]
using System;
using System.Drawing;
using System.Windows.Forms;

namespace PMD.Library.WinFormControls
{
public class PMDDataGridView : DataGridView
{
// private Color _blurredSelectionBackGround = Color.Red;

protected override void OnGotFocus(EventArgs e)
{
Console.WriteLine("OnGotFocus called");
this.DefaultCellStyle.SelectionBackColor = Color.Blue;

/*
if (this.CurrentCell != null)
{
if (this.SelectionMode == DataGridViewSelectionMode.FullRowSelect)
{

this.Rows[this.CurrentCell.RowIndex].DefaultCellStyle.SelectionBackColor =
this.DefaultCellStyle.SelectionBackColor;
}
else
{
this.CurrentCell.Style.SelectionBackColor =
this.DefaultCellStyle.SelectionBackColor;
}
}
*/

base.OnGotFocus(e);
}

protected override void OnLostFocus(EventArgs e)
{
Console.WriteLine("OnLostFocus called");
this.DefaultCellStyle.SelectionBackColor = Color.Red;

/*
if (this.CurrentCell != null)
{
if (this.SelectionMode == DataGridViewSelectionMode.FullRowSelect)
{

this.Rows[this.CurrentCell.RowIndex].DefaultCellStyle.BackColor =
this._blurredSelectionBackGround;
}
else
{
this.CurrentCell.Style.SelectionBackColor = this._blurredSelectionBackGround;
}
}
*/

base.OnLostFocus(e);
}
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGridView - Change selection color when focus lost Steve K Microsoft C# .NET 0 30th Dec 2008 08:55 AM
SubForm: Preserving Selection Rectangle When Focus Lost? (PeteCresswell) Microsoft Access 6 11th Jun 2007 05:34 PM
Detecting selection change in ComboBox column of a DataGridView David Jackson Microsoft C# .NET 2 14th Apr 2007 07:34 PM
Selection hides when Excel lost focus =?Utf-8?B?Tmlja2U=?= Microsoft Excel Programming 2 3rd Apr 2007 09:58 AM
Datagridview Selection Change Michel Lapointe Microsoft C# .NET 0 17th Mar 2006 02:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:45 AM.