要求是:
单击gridview一行后,该行变色显示,再次单击该行,该行变回原来的颜色,如果再次点击的是另一行,另一行变色,同时原来选中的那行颜色不还原。即可以选中多行。
求代码……
PS:慕白大大的
e.Row.Attributes.Add("onclick", "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#e6c5fc';window.oldtr=this");
满足不了需求,或者需要修改一下。
召唤达人帮忙。
3Q.

解决方案 »

  1.   

    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#e6dcff'");
    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
      

  2.   

    e.Row.Attributes.Add("onclick", "this.style.backgroundColor=(this.style.backgroundColor == '#e6c5fc') ? '#FFFFFF' : '#e6c5fc';");
      

  3.   

    e.Row.Attributes.Add("onclick", "this.style.backgroundColor!="" ? '' : 'red';");
      

  4.   

    虽然两颗**的已经满足我说的要求,
    但是没这么简单的.....
    我交代的不清楚.....
    这个gridview的是交替行色的,不能简单的替换为#FFFFFF、''等颜色的。
    我用e=this.style.backgroundColor;this.style.backgroundColor=((this.style.backgroundColor == '#e6c5fc')?e:'#e6c5fc');");
    这样不行啊……继续求解。。
      

  5.   

    搞了半天。。
    不过还是搞出来了。。楼主需要给datagridview 设置如下属性:ReadOnly = true;                ---这上点是为了不让用户编辑单元格。因为用户编辑单元格时被编辑的单元格会变色的。。SelectionMode = FullRowSelect;   ---为了让用户一下选择一行数据。。DefaultCellStyle 属性里的:SelectionBackColor与其中的BackColor一样,SelectionForeColor属性最好为黑色。如果还想让您的DataGridView好看一点,就把RowHeadersVisible和AllowUserToAddRows属性都设为False
    好。。关键时候来了:在您的类中加入如下代码DataGridViewCellStyle defau ;    //单元格的默认样式
            DataGridViewCellStyle clicked = new DataGridViewCellStyle();  //用户点击一次后的样式
            private void Form1_Load(object sender, EventArgs e)
            {
                DataTable tt = new DataTable();
                DataColumn cc;
                for (int i = 0; i < 5; i++)
                {
                    cc = new DataColumn("tt"+i,typeof(string));
                    tt.Columns.Add(cc);
                }
                for (int i = 0; i < 10; i++)
                {
                    DataRow newRow = tt.NewRow();
                    newRow[0] = "efe";
                    newRow[1] = "efe";
                    newRow[2] = "efe";
                    newRow[3] = "efe";
                    newRow[4] = "efe";
                    tt.Rows.Add(newRow);
                }
                this.dataGridView1.DataSource = tt;   //绑定数据源            clicked.BackColor = Color.Red;    //设置用户点击后的颜色为红色            defau = new DataGridViewCellStyle(this.dataGridView1.Rows[0].DefaultCellStyle);//获得默认样式
            }        /// <summary>
            /// CellMouseClick事件。
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (e.RowIndex >= 0)
                {
                    DataGridViewRow clickRow = dataGridView1.Rows[e.RowIndex];                //取消行的选中。。因为选中的行,系统会自动设置背景色。。这样就会把自己设置的背景色给掩盖掉
                    clickRow.Selected = false;                    if (clickRow.DefaultCellStyle == defau)
                        clickRow.DefaultCellStyle = clicked;
                    else
                        clickRow.DefaultCellStyle = defau;                //把所有的行还原,(当前点击的行除外)
                    foreach (DataGridViewRow row in dataGridView1.Rows) 
                    {
                        if (row.Index != e.RowIndex)
                            row.DefaultCellStyle = defau;
                    }
                }
            }
      

  6.   

    e.Row.Attributes.Add("onclick", "this.style.backgroundColor=(this.style.backgroundColor == '#e6c5fc') ? '#FFFFFF' : '#e6c5fc';");
    正解
      

  7.   

    我自己搞出来了。
    不过谢谢大家了。
    我把我代码贴出来。
    JS事件:
      var OldRowBgColor;
      var NewRowBgColor  = "#B5C5E6";
      var MarkRowBgColor = "#ff9933";
      function ChangeRowBg(row)
      {
    if (event.type=='mouseover')
    {
    OldRowBgColor = row.style.backgroundColor
    row.style.backgroundColor = NewRowBgColor }
    else if (event.type=='mouseout')
    {
        row.style.backgroundColor = OldRowBgColor;
    }
      }
      function ChangeRowBgEven(row)
      {
     if (event.type=='mousedown')
    {
    if (row.style.backgroundColor != MarkRowBgColor)
    {
    row.style.backgroundColor = MarkRowBgColor;
    row.onmouseout = function(){return false;}
    row.onmouseover= function(){return false;}
        }
        else
        {
    row.style.backgroundColor ='';
        }
    }
      }
      function ChangeRowBgImpair(row)
      {
     if (event.type=='mousedown')
    {
    if (row.style.backgroundColor != MarkRowBgColor)
    {
    row.style.backgroundColor = MarkRowBgColor;
    row.onmouseout = function(){return false;}
    row.onmouseover= function(){return false;}
        }
        else
        {
    row.style.backgroundColor = NewRowBgColor;
        }
    }
      }gridview事件:
     public static void ChangeRowBg(DataGrid grdTable)
            {
                for (int i = 0; i < grdTable.Items.Count; i++)
                {
                    grdTable.Items[i].Attributes.Add("onmouseover", "ChangeRowBg(this)");
                    grdTable.Items[i].Attributes.Add("onmouseout", "ChangeRowBg(this)");
                    if (i % 2 == 0)
                        grdTable.Items[i].Attributes.Add("onmousedown", "ChangeRowBgEven(this)");
                    else
                        grdTable.Items[i].Attributes.Add("onmousedown", "ChangeRowBgImpair(this)");
                }
            }
    调用的时候直接绑定下就OK。