我希望我的dataGridView的活动记录有一些背景颜色。因此,我在代码中使用了RowEnter、RowLeave方法:
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor =
Color.FromArgb(231, 255, 231);
}
private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Empty;
}所有的工作,但非常,非常慢。在WinForms中有没有更有效的方法来实现这一效果?
发布于 2013-10-09 16:56:12
也许你可以使用javascript来提高效率。
在下面的代码中尝试一下
protected void MyGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
string rowStyle = "this.style.backgroundColor
= 'yellow'";
string rowStyleClickedTwice =
"this.style.backgroundColor = 'blue'";
string rowID = String.Empty;
if (e.Row.RowType == DataControlRowType.DataRow)
{
rowID = "row"+e.Row.RowIndex;
e.Row.Attributes.Add("id",
"row"+e.Row.RowIndex);
e.Row.Attributes.Add("onclick",
"ChangeRowColor(" +"'" + rowID + "'" + ")");
}
}你可以在下面配置你的javascript:
<input type="hidden" id="hiddenColor" />
<script language ="javascript" type="text/javascript">
document.body.style.cursor = 'pointer';
function ChangeRowColor(rowID)
{
var color = document.getElementById(rowID).style.backgroundColor;
alert(color);
if(color != 'yellow')
document.getElementById("hiddenColor").style.backgroundColor = color;
alert(oldColor);
if(color == 'yellow')
document.getElementById(rowID).style.backgroundColor = document.getElementById("hiddenColor").style.backgroundColor;
else
document.getElementById(rowID).style.backgroundColor = 'yellow';
}
</script>只要让我知道它什么时候起作用
发布于 2013-10-09 17:06:09
也许你可以试试这个:
class MyDataGridView : DataGridView
{
private int mMousedOverColumnIndex = int.MinValue;
private int mMousedOverRowIndex = int.MinValue;
protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)
{
mMousedOverColumnIndex = e.ColumnIndex;
mMousedOverRowIndex = e.RowIndex;
base.OnCellMouseEnter(e);
base.Refresh();
}
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
if (((e.ColumnIndex == mMousedOverColumnIndex) && (e.RowIndex == -1)) ||
((e.ColumnIndex == -1) && (e.RowIndex == mMousedOverRowIndex)))
{
PaintColumnHeader(e, System.Drawing.Color.Red);
}
base.OnCellPainting(e);
}
private void PaintColumnHeader(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, System.Drawing.Color color)
{
LinearGradientBrush backBrush = new LinearGradientBrush(new System.Drawing.Point(0, 0), new System.Drawing.Point(100, 100), color, color);
e.Graphics.FillRectangle(backBrush, e.CellBounds);
DataGridViewPaintParts parts = (DataGridViewPaintParts.All & ~DataGridViewPaintParts.Background);
e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;
e.AdvancedBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
e.Paint(e.ClipBounds, parts);
e.Handled = true;
}
}如果你觉得这个管用,请告诉我
发布于 2015-02-03 17:14:30
最快的方法:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if ((e.State & DataGridViewElementStates.Selected) != 0)
e.CellStyle.SelectionBackColor = Color.Green;
}https://stackoverflow.com/questions/19267239
复制相似问题