首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataGridView BackColor

DataGridView BackColor
EN

Stack Overflow用户
提问于 2013-10-09 16:52:18
回答 3查看 126关注 0票数 0

我希望我的dataGridView的活动记录有一些背景颜色。因此,我在代码中使用了RowEnter、RowLeave方法:

代码语言:javascript
复制
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中有没有更有效的方法来实现这一效果?

EN

回答 3

Stack Overflow用户

发布于 2013-10-09 16:56:12

也许你可以使用javascript来提高效率。

在下面的代码中尝试一下

代码语言: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:

代码语言: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>

只要让我知道它什么时候起作用

票数 0
EN

Stack Overflow用户

发布于 2013-10-09 17:06:09

也许你可以试试这个:

代码语言:javascript
复制
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;
        }
    }

如果你觉得这个管用,请告诉我

票数 0
EN

Stack Overflow用户

发布于 2015-02-03 17:14:30

最快的方法:

代码语言:javascript
复制
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if ((e.State & DataGridViewElementStates.Selected) != 0)
        e.CellStyle.SelectionBackColor = Color.Green;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19267239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档