在datagridview当鼠标移到某行时,该行改变颜色,有高手帮忙写个吗。(winform中)
加急

解决方案 »

  1.   

    给你代码你参考下:双击GridView的OnRowDataBound事件;
    在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int i;
            //执行循环,保证每条数据都可以更新
            for (i = 0; i < GridView1.Rows.Count; i++)
            {
                //首先判断是否是数据行
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //当鼠标停留时更改背景色
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
                    //当鼠标移开时还原背景色
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                }
            }
        }
      

  2.   

    楼上的大哥,你这是Web的,这样的网上一大把.现在是winform.我也想弄.一直没找到合适.
      

  3.   

    4楼的大哥,你看清楚问题啊,winform的
    不是web的
      

  4.   


     if (e.Row.Selected == true)
    {
         DataGridViewCellStyle style=new DataGridViewCellStyle();
         style.BackColor=Color.Red;
         dataGridView.RowsDefaultCellStyle = style;
    }鼠标移过的我没有找到这个事件.只有选中时的.我也想求鼠标移过的.呵呵
      

  5.   

    在MouseUp事件写,鼠标点过,也行,怎么写啊
      

  6.   

    具体的好像是 SelectedColor 和 SelectedForeColor
      

  7.   


            private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex >= 0)
                {
                    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
                }
            }        private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex >= 0)
                {
                    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
                }
            }
      

  8.   

    private void gv_All_List_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex >= 0)
                {
                    this.gv_All_List.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
                }
            }        private void gv_All_List_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
            {
                 if (e.RowIndex >= 0)
                {
                    for (int i = 1; i < this.gv_All_List.Rows.Count; )
                    {
                        this.gv_All_List.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.WhiteSmoke;                    i += 2;
                    }
                    this.gv_All_List.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
                }
            }
      

  9.   

    //鼠标经过
            private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
            {
                if(e.RowIndex>=0)
                {
                if (e.RowIndex != -1 && e.ColumnIndex != -1)
                {
                    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
                }
                }
            }
            //鼠标离开
            private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex >= 0)
                {
                   if(e.RowIndex != -1 && e.ColumnIndex != -1 )
                    {
                        this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.WhiteSmoke;
        
                    }
                    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;            }
      

  10.   

    //鼠标经过
            private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
            {
                if(e.RowIndex>=0)
                {
                if (e.RowIndex != -1 && e.ColumnIndex != -1)
                {
                    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
                }
                }
            }
            //鼠标离开
            private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex >= 0)
                {
                   if(e.RowIndex != -1 && e.ColumnIndex != -1 )
                    {
                        this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.WhiteSmoke;
        
                    }
                    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;            }