有两张表分别为cktz简写用c表示,js_ffry_js简写用j表示。
c表中 有个分发人员字段   例如xh为A的分发人员值为:张三、王五、李四、龙九
j表中 有个接收人员字段   例如xh为A的人员接收值为:张三,龙九问:怎样在查询c表的时候分发人员字段中  值为 张三、龙九的用红色表示?
下面是我的代码
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                string xh2 = e.Row.Cells[2].Text;
                string name2 = e.Row.Cells[22].Text;                string SqlConn = System.Configuration.ConfigurationSettings.AppSettings["ConnString"];
                SqlConnection Conn = new SqlConnection(SqlConn);
                Conn.Open();
                string SqlStr0 = "select distinct(xh) from js_ffry_js";
                SqlCommand Comm0 = new SqlCommand(SqlStr0, Conn);
                SqlDataReader read0 = Comm0.ExecuteReader();
                if (read0.Read())
                {
                    string xh1 = read0["xh"].ToString();
                    string SqlConn1 = System.Configuration.ConfigurationSettings.AppSettings["ConnString"];
                    SqlConnection Conn1 = new SqlConnection(SqlConn1);
                    Conn1.Open();
                    string SqlStr1 = "select distinct(ry_name) from js_ffry_js";
                    SqlCommand Comm1 = new SqlCommand(SqlStr1, Conn1);
                    SqlDataReader read1 = Comm1.ExecuteReader();
                    if (read1.Read())
                    {
                        string name1 = read1["ry_name"].ToString();
                        if(name2.Trim().Contains(name1.Trim()) && xh1==xh2)
                        {                           GridView1.Rows[i].Cells[22].ForeColor = System.Drawing.Color.Red;//这句话该怎样控制只是部分内容?
              
                    }
                    else
                    {
                        read1.Close();
                        Conn1.Close();
                    }                   
                }
                else
                {
                    read0.Close();
                    Conn.Close();
                }
            }
        
    }

解决方案 »

  1.   

    模板列
    Text='<%# GetColor((int)Eval("id"),(GridViewRow)Container) %>'
    public string GetColor(int id,GridViewRow row)
    {
    row.BackColor = Color.Red;
    return "";
    }
      

  2.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
            if (e.Row.RowIndex >= 0)
            {
               e.Row.BackColor = System.Drawing.Color.SkyBlue;//显示颜色,可自定义
            }
    }
      

  3.   

    首先,把你的业务逻辑方法写到BLL类中,例如:public static Color GetYhColor(string yh)
    {
        switch (yh)
        {
            case "张三":
            case "龙九":
                return Color.Red;
            default:
                return Color.Green;
        }
    然后界面人员(美工或者售后服务人员)就可以为设计界面增加ForeColor属性:<asp:Label ID="Label1" runat="server" Text='<%# Bind("yh") %>' ForeColor='<%# BLL.GetYhColor((string)Eval("yh")) %>'></asp:Label>
      

  4.   

    每一个职责的人做不同的事,这很重要。程序员过分纠结 GridView1_RowDataBound 事件处理程序,使得软件维护和更新越来越难。
      

  5.   

    sp1234,你好,你说的我大概明白了,我还有两个问题:
    1、case "张三":
            case "龙九":   怎样转换为SQL语句?2、
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("yh") %>' ForeColor='<%# BLL.GetYhColor((string)Eval("yh")) %>'></asp:Label>我不是在label中显示的,是在gridview中该如何显示?
      

  6.   

    获取数据库值后分割
    split再判断
    gridview模板列label
      

  7.   

    谢谢sp1234,你好.我还没有用过bll
    能大概跟我讲下吗?
      

  8.   

    bll是三层架构中的业务逻辑层(BLL)三层架构
      

  9.   

    提示无法将类型为“System.DBNull”的对象强制转换为类型“System.String”,怎么办啊