各位高手,现在有两个问题,就是datagridview需要按一个字段的值改变颜色,要如何办???同时在datagridview的点击事件中,找到这一列的FieldName与本行的对应值,如何办???

解决方案 »

  1.   

    在DataGridView的CellFormating事件里处理下
    下面的代码是假设值为True则显示为绿色,反之红色private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e.ColumnIndex > -1 && e.RowIndex > -1)
                {
                    bool bVal = (bool)e.Value;
                    if (bVal)
                    {
                        e.CellStyle.ForeColor = Color.Green;
                        e.CellStyle.SelectionForeColor = Color.Green;
                    }
                    else
                    {
                        e.CellStyle.ForeColor = Color.Red;
                        e.CellStyle.SelectionForeColor = Color.Red;
                    }
                }
      

  2.   

    第一个问题的解:
    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e.Value == null) return;
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    if (//具体的业务逻辑判断)
                    {
                        this.dataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
                    }
                }
            }
    第二个问题没太看懂,再详细的说说吧
      

  3.   

    第一个问题不说了
    说下第二个问题
    在cellclick事件里面写代码;
    得到字段名称:datagridview1.columns[e.columnsindex].name;
    得到该单元格的值:datagridview1.rows[e.rowsindex].cells["得到的字段名称"].value;
    搞两个变量把上面的值保存起来,想怎么用就怎么用了。