0

我希望我的 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 中实现这种效果?

4

3 回答 3

0

最快的方法:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if ((e.State & DataGridViewElementStates.Selected) != 0)
        e.CellStyle.SelectionBackColor = Color.Green;
}
于 2015-02-03T09:14:29.873 回答
0

也许你可以试试这个:

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;
        }
    }

如果你觉得这行得通,请告诉我

于 2013-10-09T09:06:09.213 回答
0

也许你可以使用 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-09T08:56:11.500 回答