日期:2014-05-19  浏览次数:20712 次

gridview行变色问题。
要求是:
单击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.

------解决方案--------------------
e.Row.Attributes.Add( "onmouseover ", "c=this.style.backgroundColor;this.style.backgroundColor= '#e6dcff ' ");
e.Row.Attributes.Add( "onmouseout ", "this.style.backgroundColor=c; ");
------解决方案--------------------
先留个脚印~~
帮楼主顶下贴了..
------解决方案--------------------
e.Row.Attributes.Add( "onclick ", "this.style.backgroundColor=(this.style.backgroundColor == '#e6c5fc ') ? '#FFFFFF ' : '#e6c5fc '; ");
------解决方案--------------------
不好意思没看清楚

------解决方案--------------------
e.Row.Attributes.Add( "onclick ", "this.style.backgroundColor!= " " ? ' ' : 'red '; ");
------解决方案--------------------
关注中,顶一下
------解决方案--------------------
mark
------解决方案--------------------
路过,帮顶
------解决方案--------------------
搞了半天。。
不过还是搞出来了。。

楼主需要给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)