Winfrom inherits the dataGridView class to achieve the highlighted effect when the mouse passes
Winform C #17:44:16 read 60 comments 0 font size: large, medium, and small
In Winfrom programming, why do I suggest using my own controls? In this way, you can modify and add any modifications in a unified manner. This article introduces how to inherit the highlighted effect when the mouse of the dataGridView is passed. The Code is as follows:
Class GridView: System. Windows. Forms. DataGridView
{
# Region Andy 'coDe
# Region attribute code
/// <Summary>
/// Color before entering Cells
/// </Summary>
Private static System. Drawing. Color _ BeforInCellsColor;
/// <Summary>
/// Highlight when the mouse passes
/// </Summary>
Private static System. Drawing. Color _ MouseOverHighColor = System. Drawing. Color. SkyBlue;
[System. ComponentModel. Browsable (true)]
[System. ComponentModel. Description ("highlight when the mouse passes")]
[System. ComponentModel. DefaultValue (typeof (System. Drawing. Color), "SkyBlue")]
[System. ComponentModel. Category ("OtherStyle")]
Public System. Drawing. Color MouseOverHighColor
{
Get {return GridView. _ MouseOverHighColor ;}
Set {GridView. _ MouseOverHighColor = value ;}
}
/// <Summary>
/// Whether to enable the highlighted color when the mouse goes through. If enabled, SelectionMode will also be set to FullRowSelect
/// </Summary>
Private bool _ UseMouseOverStyle = false;
[System. ComponentModel. Browsable (true)]
[System. ComponentModel. Category ("OtherStyle")]
[System. ComponentModel. Description ("whether to enable color highlighting when the mouse goes through. If so, SelectionMode will also be set to FullRowSelect")]
[System. ComponentModel. DefaultValue (false)]
Public bool UseMouseOverStyle
{
Get {return _ UseMouseOverStyle ;}
Set
{
If (value)
This. SelectionMode = maid. FullRowSelect;
Else
This. SelectionMode = maid. RowHeaderSelect;
_ UseMouseOverStyle = value;
}
}
Private static Color _ BeforCellSelect;
# Endregion
# Region rewrite Method
Protected override void OnDataBindingComplete (DataGridViewBindingCompleteEventArgs e)
{
Base. OnDataBindingComplete (e );
If (this. Rows. Count> 0)
{
For (int I = 0; I <this. Rows. Count; I ++)
{
This. Rows [I]. HeaderCell. Value = (I + 1). ToString ();
}
If (this. SelectedRows. Count> 0)
SelectedRows [0]. Selected = false;
}
}
Protected override void OnCellMouseLeave (maid e)
{
Base. OnCellMouseLeave (e );
If (UseMouseOverStyle)
{
If (e. RowIndex>-1 & e. ColumnIndex>-1)
{
If (! This. Rows [e. RowIndex]. Selected)
{
This. Rows [e. RowIndex]. DefaultCellStyle. BackColor = _ BeforInCellsColor;
}
}
}
}
Protected override void OnCellMouseEnter (maid e)
{
Base. OnCellMouseEnter (e );
If (UseMouseOverStyle)
{
If (e. RowIndex>-1 & e. ColumnIndex>-1)
{
If (! This. Rows [e. RowIndex]. Selected)
{
_ BeforInCellsColor = this. Rows [e. RowIndex]. DefaultCellStyle. BackColor;
This. Rows [e. RowIndex]. DefaultCellStyle. BackColor = _ MouseOverHighColor;
}
}
}
}
Protected override void OnRowLeave (maid e)
{
Base. OnRowLeave (e );
This. Rows [e. RowIndex]. DefaultCellStyle. BackColor = _ BeforCellSelect;
}
Protected override void OnCellClick (maid e)
{
Base. OnCellClick (e );
_ BeforCellSelect = _ BeforInCellsColor;
}
# Endregion
# Endregion
}
Remember to add these references
Using System. Windows. Forms;
Using System. ComponentModel;
Using System. Drawing;
Using System. DaTa;
Using System. Reflection;
After the project is generated, a component is generated in the toolbar,
You can use it directly.
Http://limingloved.blog.163.com/blog/static/132297658201031154416618/
My applications:
Add a class mydview dview that inherits from System. Windows. Forms. DataGridView
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. ComponentModel;
Using System. Drawing;
Using System. Data;
Using System. Reflection;
Namespace Solution1Test
{
Class MyDataGridView: System. Windows. Forms. DataGridView
{
# Region Andy 'Code
# Region attribute code
/// <Summary>
/// Color before entering Cells
/// </Summary>
Private static System. Drawing. Color _ BeforInCellsColor;
/// <Summary>
/// Highlight when the mouse passes
/// </Summary>
Private static System. Drawing. Color _ MouseOverHighColor = System. Drawing. Color. SkyBlue;
[System. ComponentModel. Browsable (true)]
[System. ComponentModel. Description ("highlight when the mouse passes")]
[System. ComponentModel. DefaultValue (typeof (System. Drawing. Color), "SkyBlue")]
[System. ComponentModel. Category ("OtherStyle")]
Public System. Drawing. Color MouseOverHighColor
{
Get {return MyDataGridView. _ MouseOverHighColor ;}
Set {MyDataGridView. _ MouseOverHighColor = value ;}
}
/// <Summary>
/// Whether to enable the highlighted color when the mouse goes through. If enabled, SelectionMode will also be set to FullRowSelect
/// </Summary>
Private bool _ UseMouseOverStyle = false;
[System. ComponentModel. Browsable (true)]
[System. ComponentModel. Category ("OtherStyle")]
[System. ComponentModel. Description ("whether to enable color highlighting when the mouse goes through. If so, SelectionMode will also be set to FullRowSelect")]
[System. ComponentModel. DefaultValue (false)]
Public bool UseMouseOverStyle
{
Get {return _ UseMouseOverStyle ;}
Set
{
If (value)
This. SelectionMode = maid. FullRowSelect;
Else
This. SelectionMode = maid. RowHeaderSelect;
_ UseMouseOverStyle = value;
}
}
Private static Color _ BeforCellSelect;
# Endregion
# Region rewrite Method
Protected override void OnDataBindingComplete (DataGridViewBindingCompleteEventArgs e)
{
Base. OnDataBindingComplete (e );
If (this. Rows. Count> 0)
{
For (int I = 0; I <this. Rows. Count; I ++)
{
This. Rows [I]. HeaderCell. Value = (I + 1). ToString ();
}
If (this. SelectedRows. Count> 0)
SelectedRows [0]. Selected = false;
}
}
Protected override void OnCellMouseLeave (maid e)
{
Base. OnCellMouseLeave (e );
If (UseMouseOverStyle)
{
If (e. RowIndex>-1 & e. ColumnIndex>-1)
{
If (! This. Rows [e. RowIndex]. Selected)
{
This. Rows [e. RowIndex]. DefaultCellStyle. BackColor = _ BeforInCellsColor;
}
}
}
}
Protected override void OnCellMouseEnter (maid e)
{
Base. OnCellMouseEnter (e );
If (UseMouseOverStyle)
{
If (e. RowIndex>-1 & e. ColumnIndex>-1)
{
If (! This. Rows [e. RowIndex]. Selected)
{
_ BeforInCellsColor = this. Rows [e. RowIndex]. DefaultCellStyle. BackColor;
This. Rows [e. RowIndex]. DefaultCellStyle. BackColor = _ MouseOverHighColor;
}
}
}
}
Protected override void OnRowLeave (maid e)
{
Base. OnRowLeave (e );
This. Rows [e. RowIndex]. DefaultCellStyle. BackColor = _ BeforCellSelect;
}
Protected override void OnCellClick (maid e)
{
Base. OnCellClick (e );
_ BeforCellSelect = _ BeforInCellsColor;
}
# Endregion
# Endregion
}
}
After compilation, you can generate your own control at the top of the toolbar.