我從數據庫中抓一批數據如:
IDA39C9026
IDA39C9027
IDA39C9028
IDA39C9029
IDA39C9030
IDA39C9031
我現將這批數據在Gridview中顯示:但要求其中的一行字(不是固定的第幾行)是紅色的:如IDA39C9026如行是紅色的,
IDA39C9026
IDA39C9027
IDA39C9028
IDA39C9029
IDA39C9030
IDA39C9031
我現將這批數據在Gridview中顯示:但要求其中的一行字(不是固定的第幾行)是紅色的:如IDA39C9026如行是紅色的,
解决方案 »
- win7下 C# winform 部署问题
- 哪位大侠知道怎么在matlab中 添加COM组件呢?希望能详细点呢 本人菜鸟啦
- OpenNETCF.Desktop.Communication.dll 无法加载 DLL“rapi.dll”: 动态链接库(DLL)初始化例程失败。
- 高手们 socket能实现广域网的聊天吗?
- 无聊发现倒分帖。。
- 在windows应用程序中如何实现客户端与服务器之间传送信息,非常急
- 想换c#程序学习的大侠进来看看!
- 收缩SQL数据库会丢失数据吗??
- 求获取字符串内的数字
- 在线等--WINFORM下图片显示问题
- 如何获取c#工程里的图片
- DevExpress.XtraEditors控件问题
DataTable dt = SQLHelper.GetDataTable(sb1.ToString());
GridView1.DataSource = dt;
GridView1.DataBind();
{
if (e.ColumnIndex < 0 || e.RowIndex < 0) { return; }
if (this.dataGridViewBasicCfmx.Columns[e.ColumnIndex].Name.ToUpper() == "PSJG")
{
if (this.dataGridViewBasicCfmx[e.ColumnIndex, e.RowIndex].Value.ToString() == "3")
{
this.dataGridViewBasicCfmx[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Blue;
}
else
{
if (this.dataGridViewBasicCfmx[e.ColumnIndex, e.RowIndex].Value.ToString() == "2")
{
this.dataGridViewBasicCfmx[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Red;
}
else
{
this.dataGridViewBasicCfmx[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Black;
}
}
}
}
可以根据某列的值,来定义该列的颜色,如果是所有列的颜色都要设置,则循环设置一下
{
if (e.ColumnIndex < 0 || e.RowIndex < 0) { return; }
if (this.dataGridViewBasicCfmx.Columns[e.ColumnIndex].Name.ToUpper() == "PSJG")
{
if (this.dataGridViewBasicCfmx[e.ColumnIndex, e.RowIndex].Value.ToString() == "3")
{
this.dataGridViewBasicCfmx[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Blue;
}
else
{
if (this.dataGridViewBasicCfmx[e.ColumnIndex, e.RowIndex].Value.ToString() == "2")
{
this.dataGridViewBasicCfmx[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Red;
}
else
{
this.dataGridViewBasicCfmx[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Black;
}
}
}
//编辑
protected void gvwOne_RowEditing(object sender, GridViewEditEventArgs e)
{
gvwOne.EditIndex = e.NewEditIndex;
gvwBand();//绑定
}
//编辑 protected void gvwOne_RowEditing(object sender, GridViewEditEventArgs e) { gvwOne.EditIndex = e.NewEditIndex; gvwBand();//绑定 }C#代码
//更新
protected void gvwOne_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
String strOne = gvwOne.Rows[e.RowIndex].Cells[0].Text.Trim();
访问数据库等代码...
Response.Write("<script language:javascript>javascript:window:alert('修改成功');</script>");
gvwOne.EditIndex = -1;
gvwBand();//绑定数据
}
//更新 protected void gvwOne_RowUpdating(object sender, GridViewUpdateEventArgs e) { String strOne = gvwOne.Rows[e.RowIndex].Cells[0].Text.Trim(); 访问数据库等代码... Response.Write("<script language:javascript>javascript:window:alert('修改成功');</script>"); gvwOne.EditIndex = -1; gvwBand();//绑定数据 }C#代码
//取消
protected void gvwOne_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvwOne.EditIndex = -1;
gvwBand();//绑定
}
//取消 protected void gvwOne_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gvwOne.EditIndex = -1; gvwBand();//绑定 }C#代码
//删除
protected void gvwOne_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
String strOne = gvwOne.Rows[e.RowIndex].Cells[0].Text.Trim();
访问数据库等操作...
Response.Write("<script language:javascript>javascript:window:alert('删除成功');</script>");
gvwBand();//绑定
}
//删除时弹出提示框
protected void gvwOne_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[17].Controls[2]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");//Cells[17]代表删除所在列索引,Controls[2]表示按下第三个汉字才弹出提示
}
}
//---------------鼠标经过行变色------------------
int i;
//执行循环,保证每条数据都可以更新
for (i = -1; i < gvwOne.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='red'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
//---------------鼠标经过行变色------------------
}
参考答案来源于:wenwen.soso.com我正在使用《Csdn收音机》第一时间获取最新动态!
{
if (value.Contains("IDA39C9026"))
{
container.ForeColor = Color.Red;
container.BackColor = Color.BlanchedAlmond;
container.Font.Bold = true;
}
return value;
}实际上,通过数据绑定表达式,并且我们用一个函数来实现这个表达式,你可以在函数中进行任意复杂的数据加工,对界面显示进行任意复杂的改头换面。
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
if ( GridView1.Rows[i].Cells[j].Text==“IDA39C9026”)
{
GridView1.Rows[i].Cells[j].BackColor = System.Drawing.Color.Red;
}
}变量j为你需要变色的所在列
重要的是思想,理解原理。如果理解不了就没办法了。如果理解了,拼凑、抄袭的那些一大堆代码当作垃圾扔一旁就行了。
不要想当然地使用j,以及Text属性,因为软件设计者的本职工作就是经常调整列的次序、以及列的模板内容。写程序应该写可以长期使用的,不制造麻烦的程序。
sorry,是#6楼写的,不是#9楼。
http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
Object info = e.Row.DataBoundItem;//取得行所绑定的数据源 if (info == "IDA39C9026")//条件筛选。
{
e.Row.DefaultCellStyle.ForeColor = Color.Red;//整个行改变颜色.
e.Row.Cells["clmA"].Style.BackColor = Color.Red;//某一单元格改变颜色.
}
}