DataGridView DefaultCellStyle防止BackColor更改

时间:2014-10-13 18:56:30

标签: c# winforms datagridview backcolor

我正在做的是比较检查变化的两个数据表。如果表格单元格值发生更改,请通过更改其BackColor属性来标记相应的DataGridView单元格。这完全有效,直到我需要设置DataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True和DataGridView.AutoSizeRowsMo​​de = DataGridViewAutoSizeRowsMo​​de.AllCells。我需要这样做,所以我可以将文本转到单元格内的新行。 Here是StackOverflow的答案,可以解释这一点。因此在设置DefaultCellStyle后,我无法更改BackColor。但是我有一个Validator类,它传入一个DataGridViewCell并验证该值。如果单元格中的文本无效或格式正确,则Style.BackColor会发生变化......这样做!我的问题是为什么我的Validator类可以改变BackColor但我不能直接从表单中删除?以下是我正在做的和我正在经历的事情的简化示例:

    private void initDataGridView()
    {
        DataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
        DataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        DataGridView.MultiSelect = false;
        DataGridView.AllowUserToAddRows = false;
        DataGridView.AllowUserToDeleteRows = false;
        DataGridView.RowHeadersVisible = false;

        // ADD EVENT HANDLERS
        DataGridView.CellEndEdit += DataGridView_CellEndEdit;
    }

    private void compareTables()
    {
        string dataTableCell;
        string dataTableMirrorCell;

        for (int rowIndex = 0; rowIndex < dataTable.Rows.Count; rowIndex++)
        {
            for (int colIndex = 2; colIndex < dataTable.Columns.Count; colIndex++)
            {
                // initialize cell values to compare
                dataTableCell = dataTable.Rows[rowIndex].Field<string>(colIndex);
                dataTableMirrorCell = dataTableMirror.Rows[rowIndex].Field<string>(colIndex);

                // now compare cell values
                if (dataTableCell != dataTableMirrorCell)
                {
                    if (dataTableCell== null
                        || dataTableCell== string.Empty)
                    {
                        dataGridView.Rows[rowIndex].Cells[colIndex].Style.BackColor = Color.Red;
                        dataGridView.Rows[rowIndex].Cells[colIndex].Tag = "Delete";
                    }
                    else
                    {
                        if (dataTableMirrorCell == null
                            || dataTableMirrorCell == string.Empty)
                        {
                            dataGridView.Rows[rowIndex].Cells[colIndex].Style.BackColor = Color.Orange;
                            dataGridView.Rows[rowIndex].Cells[colIndex].Tag = "Add";
                        }
                        else
                        {
                            dataGridView.Rows[rowIndex].Cells[colIndex].Style.BackColor = Color.Yellow;
                            dataGridView.Rows[rowIndex].Cells[colIndex].Tag = "Change";
                        }
                    }
                }
            }
        }
    }

    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        DataGridView dgv = (DataGridView)sender;
        Validator validator = new Validator(this);

        if (e.ColumnIndex != dgv.Columns["Name"].Index)
        {
            DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];

            // Check the value for valid format
            validator.validateCell(cell, ValidationType.Numeric);               
        }
    }

这是Validator.validateCell方法:

    public void validateCell(DataGridViewCell cell, String type)
    {
        // NUMERIC
        if (type.ToLower() == "numeric")
        {
            int result;
            if (!int.TryParse(cell.Value.ToString(), out result))
            {
                cell.Style.BackColor = Color.Beige;
                cell.Style.Font = new Font(this.form.Font, FontStyle.Bold);
                cell.ToolTipText = "This is a " + type + " only field.";
                cell.Tag = "invalid";
            }
            else
            {
                cell.Style.BackColor = Color.White;
                cell.Style.Font = new Font(this.form.Font, FontStyle.Regular);
                cell.ToolTipText = string.Empty;
                cell.Tag = "valid";
            }
        }
    }

Validator.validateCell WORKS,它更改单元格BackColor和Font。有人可以解释为什么本地方法compareTables不会改变BackColor和我的validator.validateCell吗?非常感谢帮助我学习!

1 个答案:

答案 0 :(得分:1)

我一直用:

dataGridView.RowsDefaultCellStyle.SelectionBackColor = Color.Red;

设置或获取dataGridView背景颜色。不确定这是否有助于您从另一个方向看