如根据datagridview  某一列的字段值,  如列1 = “值1”  的行  设置背景颜色    其他值得不便

解决方案 »

  1.   

    protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
        {
            //为包含有特定值的行改变样式         
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {//为了对全部数据行都有用             
               // string strlbl = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "[GMargin%]"));//取出行中City字段绑定的值   
                string strlbl = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "[GMargin%]"));//取出行中City字段绑定的值   
                strlbl = strlbl.Replace("%", "");
                if (strlbl!="")
                {
                    double lbl = Convert.ToDouble(strlbl);
                    if (lbl > 10)
                    {//如果他的值大于10              
                        e.Row.BackColor = System.Drawing.Color.Orange ;
                    }
                }
                   
               
            }     
        }
      

  2.   


    private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
            {
                if (e.RowIndex >= dataGridView1.Rows.Count - 1)
                    return;
                DataGridViewRow dgr = dataGridView1.Rows[e.RowIndex];
                try
                {
                  //dgr.Cells[0]是当前性别列的索引值,用以确定判断哪一列的值
                    if (dgr.Cells[0].Value.ToString() == "男")
                    {
                        //定义画笔,使用颜色是深灰。
                        using (SolidBrush brush = new SolidBrush(Color.DarkGray))
                        {
                            //利用画笔填充当前行
                            e.Graphics.FillRectangle(brush, e.RowBounds);
                            //将值重新写回当前行。
                            e.PaintCellsContent(e.ClipBounds); 
                            e.Handled = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }        }
      

  3.   


    楼主,换个CommBox 不是更好? 下拉列表框。