动态更改 DataGridViewComboBoxCell 颜色(样式)

发布于 2024-12-01 20:26:01 字数 2922 浏览 0 评论 0原文

我有一个包含几列的 DataGridview,其中一列是 DataGridViewComboBoxColumn。

场景是,当用户从组合框中选择某个值(所选索引> 0)时,所选单元格的整行将显示为白色。如果用户为组合框选择空值(所选索引为 0),则整行将显示为黄色,并且该组合框单元格应显示为红色。

我可以实现将除组合框列之外的整行显示为黄色。

private void grdCurve_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            //for datatype column
            if (grdCurve.CurrentCell.ColumnIndex == DATATYPE_COLUMN_INDEX)
            {
                // Check box column
                ComboBox comboBox = e.Control as ComboBox;
                comboBox.SelectedIndexChanged -= new EventHandler(comboBox_SelectedIndexChanged);
                comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
            }
        }

void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
        if (selectedIndex >= 0)
            {
            if (!ValidateMnemonic(mnRow, row))
                {
                    MarkInvalidRow(row);
                }
                else
                {
                    MarkValidRow(row);
                }
            }
        }

private void MarkInvalidRow(DataGridViewRow row)
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            if (m_InvalidRowTable.Count > 0)
            {
                if (m_InvalidRowTable.ContainsKey(row.Index))
                {
                    int col = Convert.ToInt32(m_InvalidRowTable[row.Index]);
                    DataGridViewCell cell = row.Cells[col];
                    MarkInvalidRowColor(row);
                    style.BackColor = Color.Red;
                    style.SelectionBackColor = Color.Red;
                    cell.Style = style;
                    if (grdCurve.CurrentCell is DataGridViewComboBoxCell)
                    {                        
                        grdCurve.CurrentCell.Style = style;
                    }
                }
                else
                {
                    MarkInvalidRowColor(row);
                }
            }
            else
            {
                MarkInvalidRowColor(row);
            }

            m_InvalidRowTable.Remove(row.Index);
        }

private void grdCurve_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //do nothing  
        }

private void grdCurve_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (grdCurve.CurrentCell is DataGridViewCheckBoxCell)
                grdCurve.CommitEdit(DataGridViewDataErrorContexts.Commit);

            if (grdCurve.CurrentCell is DataGridViewComboBoxCell && grdCurve.IsCurrentCellDirty)
                grdCurve.CommitEdit(DataGridViewDataErrorContexts.Commit);

            grdCurve.EndEdit();
        }

当我将组合框中的项目更改为空时,我希望组合框被标记为红色。 但是,它以白色显示,当我单击其他某个单元格时,组合框单元格中的红色会更新。

请帮忙。

谢谢, 普拉萨德

I have a DataGridview with several columns, and one of the column is DataGridViewComboBoxColumn.

The scenario is, When the user selects some value from the combobox (selected index > 0), the whole row of the selected cell will be shown in white. If the user selects the empty value for the combobox (selected index is 0) then the whole row will be shown in Yellow, and this combobox cell should be shown in Red.

I could able to achieve showing the whole row in yellow except for the combobox column.

private void grdCurve_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            //for datatype column
            if (grdCurve.CurrentCell.ColumnIndex == DATATYPE_COLUMN_INDEX)
            {
                // Check box column
                ComboBox comboBox = e.Control as ComboBox;
                comboBox.SelectedIndexChanged -= new EventHandler(comboBox_SelectedIndexChanged);
                comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
            }
        }

void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
        if (selectedIndex >= 0)
            {
            if (!ValidateMnemonic(mnRow, row))
                {
                    MarkInvalidRow(row);
                }
                else
                {
                    MarkValidRow(row);
                }
            }
        }

private void MarkInvalidRow(DataGridViewRow row)
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            if (m_InvalidRowTable.Count > 0)
            {
                if (m_InvalidRowTable.ContainsKey(row.Index))
                {
                    int col = Convert.ToInt32(m_InvalidRowTable[row.Index]);
                    DataGridViewCell cell = row.Cells[col];
                    MarkInvalidRowColor(row);
                    style.BackColor = Color.Red;
                    style.SelectionBackColor = Color.Red;
                    cell.Style = style;
                    if (grdCurve.CurrentCell is DataGridViewComboBoxCell)
                    {                        
                        grdCurve.CurrentCell.Style = style;
                    }
                }
                else
                {
                    MarkInvalidRowColor(row);
                }
            }
            else
            {
                MarkInvalidRowColor(row);
            }

            m_InvalidRowTable.Remove(row.Index);
        }

private void grdCurve_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //do nothing  
        }

private void grdCurve_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (grdCurve.CurrentCell is DataGridViewCheckBoxCell)
                grdCurve.CommitEdit(DataGridViewDataErrorContexts.Commit);

            if (grdCurve.CurrentCell is DataGridViewComboBoxCell && grdCurve.IsCurrentCellDirty)
                grdCurve.CommitEdit(DataGridViewDataErrorContexts.Commit);

            grdCurve.EndEdit();
        }

When i change the item in the combobox to empty, I expect the combobox to be marked in red color.
But, it shows in white and when i click on some other cell, the red color gets updated in the combobox cell.

Please help.

Thanks,
Prasad

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯狂的代价 2024-12-08 20:26:01

看看这里 http://msdn.microsoft.com/en-us/library /1yef90x0.aspx 有一个标题为动态设置单元格样式的部分。您应该为 DataGridView.CellFormatting 实现一个处理程序

Take a look here http://msdn.microsoft.com/en-us/library/1yef90x0.aspx theres is a section entitled setting cell styles dynamically. You should implement a handler for DataGridView.CellFormatting

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文