DataGridView隔行变色鼠标经过改变形状及行变色
添加MouseEnter()与MouseLeave()事件。
url:http://greatverve.cnblogs.com/archive/2012/08/09/DataGridView-MouseEnter-MouseLeave.html
/// <summary>
/// 用来记录先前的颜色值
/// </summary>
Color colorTmp = Color.White;
/// <summary>
/// 记录鼠标形状
/// </summary>
Cursor cursorTmp = Cursor.Current;
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
colorTmp = dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor;
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Silver;
if (e.ColumnIndex == 1)//改变第二列鼠标形状
{
cursorTmp = this.Cursor;
this.Cursor = Cursors.Hand;
}
}
}
private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = colorTmp;
if (e.ColumnIndex == 1)
{
this.Cursor = cursorTmp;
}
}
}
/// <summary>
/// 这个函数没有成功,设置AlerternatingRowDefaultCellStyle属性成功
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (i % 2 == 0)
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Gainsboro; ;
}
else
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.PowderBlue;
}
}
}
/// 用来记录先前的颜色值
/// </summary>
Color colorTmp = Color.White;
/// <summary>
/// 记录鼠标形状
/// </summary>
Cursor cursorTmp = Cursor.Current;
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
colorTmp = dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor;
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Silver;
if (e.ColumnIndex == 1)//改变第二列鼠标形状
{
cursorTmp = this.Cursor;
this.Cursor = Cursors.Hand;
}
}
}
private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = colorTmp;
if (e.ColumnIndex == 1)
{
this.Cursor = cursorTmp;
}
}
}
/// <summary>
/// 这个函数没有成功,设置AlerternatingRowDefaultCellStyle属性成功
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (i % 2 == 0)
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Gainsboro; ;
}
else
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.PowderBlue;
}
}
}
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。