• C# DataGridView控件 修改一行或单元格的颜色


    1.winform datagridview更改选中行中选中单元格的背景或字体颜色

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected) { DataGridViewCell aa = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; aa.Style.ForeColor = Color.Red; aa.Style.BackColor = Color.LightGreen; } }

    2.winfrom——DataGridView 选中某一行的事件

    先将SelectionMode属性设置一下,改为fullrowselection. 然后给一个cellclick事件 注意:点击表头时也会触发此事件,在取值时要排除

    private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
    {
      //当点击表头部的列时,e.RowIndex==-1
      if (e.RowIndex > -1)
      {
        this.txtUsername.Text = this.dataGridView2.Rows[e.RowIndex].Cells[1].Value.ToString();
        this.txtPassword.Text = this.dataGridView2.Rows[e.RowIndex].Cells[2].Value.ToString();
        this.txtPosition.Text = this.dataGridView2.Rows[e.RowIndex].Cells[3].Value.ToString();
        this.txtStatus.Text = this.dataGridView2.Rows[e.RowIndex].Cells[6].Value.ToString();
        this.txtName.Text = this.dataGridView2.Rows[e.RowIndex].Cells[7].Value.ToString();
      }
     
    }

    3.C#控件DataGridView通过一列的值改变整行颜色

    在控件DataGridView的RowPrePaint事件:

     private void DGV_staffList_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
            {
               
                for (int i = 0; i < this.DGV_staffList.Rows.Count; i++)
                {
                    if (this.DGV_staffList.Rows[i].Cells["Column14"].Value.ToString() == "1")
                    {
                        this.DGV_staffList.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
                    }
     
                }
     
            }

    转 : https://bbs.csdn.net/topics/390207713/

    https://blog.csdn.net/Pei_hua100/article/details/111565976

    https://blog.csdn.net/baidu_38995168/article/details/86629995

  • 相关阅读:
    20181123(编码问题)
    20181122笔记(列表、元组、字典和集合的数据类型及内置方法)
    20181121笔记(for,数字类型和字符串类型的内置方法)
    20181120笔记
    Python的第3堂课
    错误集合(想起来就更新)
    Python的第二堂课(2)
    Python的第二堂课(1)
    boost::bind实践2——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》
    boost::bind实践
  • 原文地址:https://www.cnblogs.com/fps2tao/p/16482219.html
Copyright © 2020-2023  润新知