To change the DataGridViewComboBoxCell color(style) dynamically

谁都会走 提交于 2019-12-11 04:33:01

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/7242308/to-change-the-datagridviewcomboboxcell-colorstyle-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!