【问题标题】:DataGridview row color change based on the job role using C#使用 C# 根据工作角色更改 DataGridview 行颜色
【发布时间】:2021-12-30 06:07:38
【问题描述】:

我正在用 C# 创建一个简单的应用程序。我可以添加记录。我可以成功查看 DataGridview 中的记录。但我被行的颜色变化困住了。

颜色需要根据员工角色进行更改,以便于识别。员工具有以下 3 个角色之一:销售经理、软件工程师、项目经理。

软件工程师员工的颜色需要显示为红色,项目经理为蓝色,销售经理为黄色。

这是我迄今为止尝试过的 - 错误显示为无效尝试

private DataTable GetDataFromDB()
            {
        
               
            }

            private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                if (dataGridView1.Columns[e.ColumnIndex].Name == "role")
                {
                    ColorRow(dataGridView1.Rows[e.RowIndex]);
                }
            }


        private void ColorAllRows() 
    {
              foreach (DataGridViewRow row in dataGridView1.Rows) {
                ColorRow(row);
              }
    }

        private void ColorRow(DataGridViewRow row)
        {
            if (row.Cells["role"].Value != null)
            {
                switch (row.Cells["role"].Value.ToString())
                {
                    case "a":
                        row.DefaultCellStyle.BackColor = Color.Yellow;
                        return;
                    case "b":
                        row.DefaultCellStyle.BackColor = Color.LightCoral;
                        return;
                    case "c":
                        row.DefaultCellStyle.BackColor = Color.LightBlue;
                        return;
                }
            }
            row.DefaultCellStyle.BackColor = Color.White;
        }

} read[2]是角色——如何使用if条件并根据角色改变网格单元格的颜色?

【问题讨论】:

  • 这能回答你的问题吗? How to change row color in datagridview?
  • 我发这篇文章之前没有解决它。我更新了上面的问题
  • 你能写这个代码吗
  • int x = dataGridView1.Rows.Add... 然后dataGridView1.Rows[x].DefaultCellStyle... ?
  • int x = int.Parse(dataGridView1.Rows.Add(read[2]).ToString()); dataGridView1.Rows[x].DefaultCellStyle.BackColor = Color.Red;仍然错误人为错误显示为无效尝试没有数据存在

标签: c#


【解决方案1】:

您应该尽量避免“手动”将行添加到网格中,如代码所示……dataGridView1.Rows.Add(read[0],read[1],read[2],read[3]);……将查询中的数据读入DataTable,然后将该表用作DataSource 到@ 987654326@。手动添加行只会为您创建“更多”工作。

关于根据“角色”单元格中的值更改行颜色,您可以使用网格CellValidating 事件或网格CellValueChanged 事件。 CellValidating 事件的触发次数可能比我们需要的要多,这就是我更喜欢使用网格CellValueChanged 事件的原因,如下所示。当用户更改单元格值并尝试离开单元格时,将触发此事件。不幸的是,设置网格DataSource 时不会触发此事件。因此,我们需要实现一个方法,循环遍历网格中的行,并在网格“设置”DataSource 之后为行着色。

因此,如果我们创建一个简单的方法,该方法采用DataGridViewRow 行并检查“角色”单元格的值,然后相应地为该行着色,则可能会简化事情。然后我们应该能够在CellValueChanged 事件中使用该方法,以及在数据加载到网格后为行着色所需的方法。这种直截了当的ColorRow 方法可能看起来像……

private void ColorRow(DataGridViewRow row) {
  if (row.Cells["Role"].Value != null) {
    switch (row.Cells["Role"].Value.ToString()) {
      case "Sales Manager":
        row.DefaultCellStyle.BackColor = Color.Yellow;
        return;
      case "Software Engineer":
        row.DefaultCellStyle.BackColor = Color.LightCoral;
        return;
      case "Project Manager":
        row.DefaultCellStyle.BackColor = Color.LightBlue;
        return;
    }
  }
  row.DefaultCellStyle.BackColor = Color.White;
}

如果给定行的“角色”单元格是 null 或不是目标角色类型之一,则该行颜色为白色。否则,该行将根据“角色”单元格值着色。

这应该会简化CellValueChanged 事件代码和我们需要根据“角色”单元格值循环遍历行和颜色的方法。事件和方法可能看起来像……

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
  if (dataGridView1.Columns[e.ColumnIndex].Name == "Role") {
    ColorRow(dataGridView1.Rows[e.RowIndex]);
  }
}

private void ColorAllRows() {
  foreach (DataGridViewRow row in dataGridView1.Rows) {
    ColorRow(row);
  }
}

为了对此进行测试,下面是一个小而完整的示例,演示了上述内容。

DataTable GridDT;

public Form1() {
  InitializeComponent();
  dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
}

private void Form1_Load(object sender, EventArgs e) {
  GridDT = GetDataFromDB();
  dataGridView1.DataSource = GridDT;
  ColorAllRows();
}

private DataTable GetDataFromDB() {
  DataTable dt = new DataTable();
  dt.Columns.Add("Name", typeof(string));
  dt.Columns.Add("Role", typeof(string));
  Random rand = new Random();
  string curRole = "";
  for (int i = 0; i < 20; i++) {
    switch (rand.Next(3)) {
      case 1:
        curRole = "Software Engineer";
        break;
      case 2:
        curRole = "Project Manager";
        break;
      default:
        curRole = "Sales Manager";
        break;
    }
    dt.Rows.Add("Name " + i, curRole);
  }
  return dt;
}

代码应该产生类似...

【讨论】:

  • 我修改了上面的代码如何从数据库中检索数据仍然无法解决问题
  • … ? ......在更新的代码中......“为什么”你使用“我的代码”来简单地创建一些“测试”数据......? ... 看来代码正在获取“真实”数据... ada1.Fill(dt); ... ? …CellValueChanged 事件在哪里…? ... ColorRow 方法在哪里,你在哪里调用它?
  • CellValueChanged ColorRow 我加了先生。我仍然收到 dt.Rows.Add(curRole); 的错误
  • 我看到了您的更改,但是第二次...“为什么”您添加了添加随机测试数据的代码...? ...这可能会起作用,但是我相信您会遇到错误。代码“已经”包含带有 ...ada1.Fill(dt); 的数据 ...使用 THAT dt 并删除将随机值添加到表中的代码。
  • 现在数据显示颜色没有改变
猜你喜欢
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 2023-04-06
  • 1970-01-01
  • 2010-11-07
  • 2015-10-09
  • 2013-10-23
相关资源
最近更新 更多