我從數據庫中抓一批數據如:
IDA39C9026
IDA39C9027
IDA39C9028
IDA39C9029
IDA39C9030
IDA39C9031
我現將這批數據在Gridview中顯示:但要求其中的一行字(不是固定的第幾行)是紅色的:如IDA39C9026如行是紅色的,

解决方案 »

  1.   

    補充一下啊!數據綁定如下
     DataTable dt = SQLHelper.GetDataTable(sb1.ToString());
     GridView1.DataSource = dt;
     GridView1.DataBind();
      

  2.   

            private void dataGridViewBasicCfmx_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                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;
                        }
                    }
                }
            }
    可以根据某列的值,来定义该列的颜色,如果是所有列的颜色都要设置,则循环设置一下
      

  3.   

    private void dataGridViewBasicCfmx_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
      {
      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;
      }
      }
      }
      

  4.   

    <%# Eval("Id").ToString().Equals("")?"<font color=\"red\">"+Eval("").ToString()+"</font>":""%>
      

  5.   

    在Gridview绑定的同时,判断是否是要显示红色的数据,如果是,给它添加颜色....
      

  6.   

    asp.net c# 对GridView编辑,删除,更新,取消的操作关键字: asp.net c# gridview 编辑 删除 更新 取消 GridView添加了编辑,删除,管理列后的操作 C#代码 
    //编辑   
    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收音机》第一时间获取最新动态!
      

  7.   

    假设你的一行有很多内容,随时可能重新设计GridView的模板,可以用不着胡乱拼凑、抄袭上无关的一堆代码,直接处理当前内容和它所在的行。例如,设计显示你的内容的模板列时这样定义:<asp:Label ID="Label4" runat="server" Text='<%# 过一下而已((string)Eval("数据列名"), (TableRow)Container) %>' />注意这里唯一多出来的东西就是Container,我们把它声明为TableRow(其实GridViewRow也可以,但是后者是继承自前者,而且声明为前者就已经足够了)。然后定义一个函数来处理Container:protected string 过一下而已(string value, TableRow container)
    {
        if (value.Contains("IDA39C9026"))
        {
            container.ForeColor = Color.Red;
            container.BackColor = Color.BlanchedAlmond;
            container.Font.Bold = true;
        }
        return value;
    }实际上,通过数据绑定表达式,并且我们用一个函数来实现这个表达式,你可以在函数中进行任意复杂的数据加工,对界面显示进行任意复杂的改头换面。
      

  8.   


          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为你需要变色的所在列
      

  9.   

    首先搞清楚你自己显示 IDA39C9026 这种数据的模板列,然后把TextBox写成绑定表达式,基本上就不可能出错了,因为后续编程就实在是太简单了。
    重要的是思想,理解原理。如果理解不了就没办法了。如果理解了,拼凑、抄袭的那些一大堆代码当作垃圾扔一旁就行了。
      

  10.   


    不要想当然地使用j,以及Text属性,因为软件设计者的本职工作就是经常调整列的次序、以及列的模板内容。写程序应该写可以长期使用的,不制造麻烦的程序。
      

  11.   


    sorry,是#6楼写的,不是#9楼。
      

  12.   

    11樓的能顯示效果,但Gridview沒有頭了,謝謝
      

  13.   

    取到某行的ID,如果GV上本来没有.可以在前边+一个lable绑定然后可见调成visible="false" 后台调用绑定的ID 对其进行颜色等其他操作的修改```
      

  14.   

    应该对你有用GridView 72般绝技:
    http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx
      

  15.   


     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;//某一单元格改变颜色.
                }
            }