2024年3月31日发(作者:)

在datagridview当鼠标移到某行时,该行改变颜色

Winform:

DataGridView属性中有个SelectMode之类的属性,可以设定是选择单元格还是选择行。

在DataGirdView属性中还有DefaultCellStyle之类的属性,可以设定选中时的背景色、字体颜色等。

private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e) //鼠标移动到某行时更改背景色

{

if (ex >= 0)

{

[ex].lor = Color.

LightBlue;

}

}

private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e) //鼠标移开时还原背景色

{

if (ex >= 0)

{

[ex].lor = ;

}

}

///

/// 鼠标移动事件处理

///

///

///

private void dataGridView1_MouseMove(object sender, MouseEventArgs e)

{

tInfo hti = t(e.X, e.Y);

//如果坐标在单元格内

if ( == )

{

//取消选择所有的选定单元格

election();

// 设置控件内所有行的颜色

for (int i = 0; i < ; i++)

{

[i].lor = ;

if (i % 2 == 0)

{

[i].lor = gb(224, 224, 224);

}

else

{

[i].lor = gb(192, 192, 192);

}

if (nt > ex)

{

//设置控件内鼠标移动到的颜色

[ex].lor = gb(255,

}

}

}

}

private void Form1_Load(object sender, EventArgs e)

{

//设置奇偶行颜色

lor = ;

lor = ;

//设置奇偶行选中的颜色

ionBackColor = ;

ionBackColor = ;

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

int i;

//执行循环,保证每条数据都可以更新

for (i = 0; i < ; i++)

{

//首先判断是否是数据行

if (e == w)

{

//当鼠标停留时更改背景色

("onmouseover",

"c=oundColor;oundColor='#00A9FF'");

//当鼠标移开时还原背景色

("onmouseout", "oundColor=c");

}

}

}

255, 192)