动态更改 DataGridViewComboBoxCell 颜色(样式)
我有一个包含几列的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这里 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