0

您好我正在尝试更改数据网格视图行的颜色。我设法为一个网格做到这一点,但由于某种原因,另一个数据网格视图不起作用。

使用选项卡控件将数据网格视图放置在 2 个不同的选项卡上。它适用于 dgvTickets,但不适用于 dgvFXTickets。单元格索引是正确的,因为我在调试期间检查过它。任何帮助都会很棒。

        dgvFXTickets.AutoGenerateColumns = true;
        dgvFXTickets.DataSource = new DataAccess.FinanceData().getFXTicketsByDate(dateTimePicker1.Value).ToList();

        dgvTickets.AutoGenerateColumns = true;
        dgvTickets.DataSource = new DataAccess.FinanceData().getTicketsByDate(dateTimePicker1.Value).ToList(); 


        foreach (DataGridViewRow r in dgvTickets.Rows)
        {
            if (r.Cells[8].Value.ToString() == "Completed")
            {
                r.DefaultCellStyle.BackColor = Color.LightGreen;
            }

            else if (r.Cells[8].Value.ToString() == "Cancelled")
            {
                r.DefaultCellStyle.BackColor = Color.LightPink;
            }
        }

        foreach (DataGridViewRow rFX in dgvFXTickets.Rows)
        {
            if (rFX.Cells[12].Value.ToString() == "Completed")
            {
                rFX.DefaultCellStyle.BackColor = Color.LightGreen;
            }

            else if (rFX.Cells[12].Value.ToString() == "Cancelled")
            {
                rFX.DefaultCellStyle.BackColor = Color.LightPink;
            }
        }
4

2 回答 2

1

发生这种情况是因为数据绑定控件在控件可见之前不会更新。

我在表单加载时使用此代码来防止此问题:

Private Sub frmForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    For Each tabPage As TabPage In Me.tabTabControl1.TabPages
        tabPage.Show()
    Next tabPage

End Sub
于 2017-09-29T13:22:17.370 回答
0

The tab page should be selected before assigning color codes on it.

tabControl1.SelectedTab = 0; //select first tab page    
foreach (DataGridViewRow r in dgvTickets.Rows)
{
        if (r.Cells[8].Value.ToString() == "Completed")
        {
            r.DefaultCellStyle.BackColor = Color.LightGreen;
        }

        else if (r.Cells[8].Value.ToString() == "Cancelled")
        {
            r.DefaultCellStyle.BackColor = Color.LightPink;
        }
    }

tabControl1.SelectedTab = 1   //select second tab 
foreach (DataGridViewRow rFX in dgvFXTickets.Rows)
    {
        if (rFX.Cells[12].Value.ToString() == "Completed")
        {
            rFX.DefaultCellStyle.BackColor = Color.LightGreen;
        }

        else if (rFX.Cells[12].Value.ToString() == "Cancelled")
        {
            rFX.DefaultCellStyle.BackColor = Color.LightPink;
        }
    }

Hope this helps

于 2017-09-29T11:18:16.377 回答