我的datagridview是隔行颜色变化的那种,就是奇数行是白色的,偶数行是灰色的。
datagridview里面有个defaultcellsytle属性,在那儿可以设置selectionbackcolor,默认的是蓝色的,
所以当我选中我的datagridview的某一行的时候,就变成蓝色的了。我现在的要求是,当选中某一行的时候,颜色没有变化,就是说奇数行还是白色,偶数行还是灰色,保持它们本来的颜色。应该怎么做?

解决方案 »

  1.   

    RowsDefaultCellStyle
    DataGridViewCellStyle { SelectionBackColor=Color [White], SelectionForeColor=Color [Black] }
      

  2.   

    --可以在FormLoad事件裏面設置,也可以直接在屬性裏面設置,以下是代碼設置
    DataGridView1.DefaultCellStyle.SelectionBackColor = Color.White
    DataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black
    DataGridView1.AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.Gray
    DataGridView1.AlternatingRowsDefaultCellStyle.SelectionForeColor = Color.Black
      

  3.   

    你用我的代碼保證不會!不過我那個是VB.Net的,你改成C#的就好。
    DataGridView1.DefaultCellStyle.SelectionBackColor = Color.White
    DataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black
    DataGridView1.AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.Gray
    DataGridView1.AlternatingRowsDefaultCellStyle.SelectionForeColor = Color.Black
      

  4.   

    DefaultCellStyle是所有Cell的,
    AlternatingRowsDefaultCellStyle只是隔行的。
    我親自測試過的,和你要求的效果一樣的。
      

  5.   

            private void Form1_Load(object sender, EventArgs e)
            {
                #region 单元格交替颜色
                this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
                this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
                #endregion
            }        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
            {
                if (dataGridView1.CurrentCell.RowIndex % 2 == 1)
                {
                    dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Beige;   
                }
                else
                {
                    dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Bisque;   
                }
            }控件隔行显示不同的颜色,
    选中后的颜色不改变,保持原来的颜色。
      

  6.   


    private void Form1_Load(object sender, EventArgs e)
    {
        //设置奇偶行颜色
        dataGridView1.RowsDefaultCellStyle.BackColor = Color.White;
        dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;    //设置奇偶行选中的颜色
        dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White ;
        dataGridView1.AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.Beige;
    }
      

  7.   

    我忘了还有一点
    我的datagrid还有一个特点
    根据datatable的某一个字段,如果为1,颜色变成红色
    选中该行的时候这个红色也要求维持原样
      

  8.   

    private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        //这里面自己判断呗
      

  9.   

    怎么才能取得这一行的颜色呢?
    好象取得的总是defaultcellsytle的selectionbackcolor
      

  10.   


                this.BackColor = dataGridView1.CurrentCell.Style.BackColor;
    好像可以,不过返回默认总的Empty,并没有返回设置的值。
      

  11.   

    还有个办法,
    就是计算出当前行在屏幕上的坐标,用api取得屏幕点的颜色!