动态更改DataGridViewComboBoxCell颜色(样式) [英] To change the DataGridViewComboBoxCell color(style) dynamically
问题描述
该方案是,当用户从组合框中选择一些值(选定的索引> 0),所选单元格的整行将以白色显示。如果用户选择组合框的空值(所选索引为0),则整行将以黄色显示,并且该组合框单元格应显示为红色。
除了combobox列之外,我可以实现黄色的整行。
private void grdCurve_EditingControlShowing(object sender,DataGridViewEditingControlShowingEventArgs e)
{
//对于数据类型列
if(grdCurve.CurrentCell.ColumnIndex == DATATYPE_COLUMN_INDEX)
{
//复选框列
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是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是DataGridViewCheckBoxCell)
grdCurve.CommitEdit(DataGridViewDataErrorContexts.Commit);
if(grdCurve.CurrentCell是DataGridViewComboBoxCell&&GrdCurve.IsCurrentCellDirty)
grdCurve.CommitEdit(DataGridViewDataErrorContexts.Commit);
grdCurve.EndEdit();
}
当我将组合框中的项目更改为空时,我希望组合框以红色标记。
但是,它显示为白色,当我点击其他单元格时,红色在组合框单元格中更新。
请帮助。
谢谢,
Prasad
看看这里 http://msdn.microsoft.com/en-us/library/1yef90x0.aspx 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. 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 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 这篇关于动态更改DataGridViewComboBoxCell颜色(样式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
实现一个处理程序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();
}