C# DataGridView背景色
我希望dataGridView的活动记录有一些背景色。 因此,我在代码中使用了rownenter、RowLeave方法:C# DataGridView背景色,c#,C#,我希望dataGridView的活动记录有一些背景色。 因此,我在代码中使用了rownenter、RowLeave方法: private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) { dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.FromArgb(231, 255, 231); }
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中有没有更有效的方法来实现这种效果?也许您可以使用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>
document.body.style.cursor='pointer';
函数ChangeRowColor(rowID)
{
var color=document.getElementById(rowID).style.backgroundColor;
警报(颜色);
如果(颜色!=“黄色”)
document.getElementById(“hiddenColor”).style.backgroundColor=颜色;
警报(旧颜色);
如果(颜色=‘黄色’)
document.getElementById(rowID).style.backgroundColor=document.getElementById(“hiddenColor”).style.backgroundColor;
其他的
document.getElementById(rowID.style.backgroundColor='yellow';
}
只要让我知道它什么时候起作用也许你可以试试这个:
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;
}
}
如果您觉得这是最快的方式,请告诉我:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if ((e.State & DataGridViewElementStates.Selected) != 0)
e.CellStyle.SelectionBackColor = Color.Green;
}
我忘了说它是WinForms格式的。谢谢,@Javalovers,我会试试这个,但恐怕这不是我想要的。鼠标与我的任务无关。我只希望活动行有额外的背景颜色。亲爱的@Peter您可以根据您的要求更改事件伙伴。我已经检查了您的代码,它可以正常工作,但同样,速度没有提高,如果鼠标位于dataGrid内,dataGrid中的所有字体都可以看到重画,并且几乎没有任何移动。一旦鼠标离开网格,所有的鼠标都会飞。我想我应该找别的地方散散步。谢谢你,@javalovers,还是一样。例如,在您的示例中,有许多新事物适合我—如何在鼠标输入时更改标题的颜色。也许您可以给我一个例子,告诉我如何在自定义dataGridView中预构建网格。下面是我如何尝试实现这一点但没有成功:
公共类DGV:DataGridView{private DataGridView textboxcolumn column1;public DGV(){InitializeClass();}void InitializeClass(){this.AutoGenerateColumns=false;column1=new-DataGridViewTextBoxColumn();this.column1.DataPropertyName=“Location”;this.column1.HeaderText=“Location”;this.column1.Name=“column1”;}}
就我个人而言,我不知道如何正确格式化我的评论…