C# 设置DataGridViewComboxCell的颜色?

C# 设置DataGridViewComboxCell的颜色?,c#,.net,c#-4.0,C#,.net,C# 4.0,我使用了这个代码片段 // Second Column: Hand to use DataGridViewComboBoxCell handToUseCell = (DataGridViewComboBoxCell)this._robotSelectionDataGridView[Common.SECOND_COLUMN, currentRow]; handToUseCell.Items.Clear(); foreach (Hand hand in robotInfo.Hands) { ha

我使用了这个代码片段

// Second Column: Hand to use
DataGridViewComboBoxCell handToUseCell = (DataGridViewComboBoxCell)this._robotSelectionDataGridView[Common.SECOND_COLUMN, currentRow];
handToUseCell.Items.Clear();
foreach (Hand hand in robotInfo.Hands)
{
  handToUseCell.Items.Add(hand.Name.ToString());
}
handToUseCell.DisplayStyle = (handToUseCell.Items.Count > 1) ? DataGridViewComboBoxDisplayStyle.ComboBox :
                                                    DataGridViewComboBoxDisplayStyle.Nothing;
handToUseCell.Value = handToUseCell.Items[0];
handToUseCell.Style.BackColor = Color.DimGray;
但背面颜色没有改变。设置cell.style.backcolor时会发生什么情况?

尝试以下内容

// Second Column: Hand to use
DataGridViewComboBoxCell handToUseCell = (DataGridViewComboBoxCell)this._robotSelectionDataGridView[Common.SECOND_COLUMN, currentRow];
handToUseCell.Items.Clear();
foreach (Hand hand in robotInfo.Hands)
{
  handToUseCell.Items.Add(hand.Name.ToString());
}
handToUseCell.DisplayStyle = (handToUseCell.Items.Count > 1) ? DataGridViewComboBoxDisplayStyle.ComboBox :
                                                    DataGridViewComboBoxDisplayStyle.Nothing;
handToUseCell.Value = handToUseCell.Items[0];
handToUseCell.Style.BackColor = Color.DimGray;
handToUseCell.DefaultCellStyle.BackColor=Color.Red

// Second Column: Hand to use
DataGridViewComboBoxCell handToUseCell = (DataGridViewComboBoxCell)this._robotSelectionDataGridView[Common.SECOND_COLUMN, currentRow];
handToUseCell.Items.Clear();
foreach (Hand hand in robotInfo.Hands)
{
  handToUseCell.Items.Add(hand.Name.ToString());
}
handToUseCell.DisplayStyle = (handToUseCell.Items.Count > 1) ? DataGridViewComboBoxDisplayStyle.ComboBox :
                                                    DataGridViewComboBoxDisplayStyle.Nothing;
handToUseCell.Value = handToUseCell.Items[0];
handToUseCell.Style.BackColor = Color.DimGray;

// Second Column: Hand to use
DataGridViewComboBoxCell handToUseCell = (DataGridViewComboBoxCell)this._robotSelectionDataGridView[Common.SECOND_COLUMN, currentRow];
handToUseCell.Items.Clear();
foreach (Hand hand in robotInfo.Hands)
{
  handToUseCell.Items.Add(hand.Name.ToString());
}
handToUseCell.DisplayStyle = (handToUseCell.Items.Count > 1) ? DataGridViewComboBoxDisplayStyle.ComboBox :
                                                    DataGridViewComboBoxDisplayStyle.Nothing;
handToUseCell.Value = handToUseCell.Items[0];
handToUseCell.Style.BackColor = Color.DimGray;
handToUseCell.CellStyle.BackColor=Color.Red

// Second Column: Hand to use
DataGridViewComboBoxCell handToUseCell = (DataGridViewComboBoxCell)this._robotSelectionDataGridView[Common.SECOND_COLUMN, currentRow];
handToUseCell.Items.Clear();
foreach (Hand hand in robotInfo.Hands)
{
  handToUseCell.Items.Add(hand.Name.ToString());
}
handToUseCell.DisplayStyle = (handToUseCell.Items.Count > 1) ? DataGridViewComboBoxDisplayStyle.ComboBox :
                                                    DataGridViewComboBoxDisplayStyle.Nothing;
handToUseCell.Value = handToUseCell.Items[0];
handToUseCell.Style.BackColor = Color.DimGray;
让我知道结果。。
希望有帮助。

您是要为下拉列表设置背景色还是元素(如图所示)本身设置背景色?我试图为下拉列表添加背景色。@Vidhya您是在windows应用程序DataGridViewComboxCell中尝试的吗?没有“DefaultCellStyle”和“CellStyle”属性。请尝试参考此文档。。。让我知道它是否有用。。
// Second Column: Hand to use
DataGridViewComboBoxCell handToUseCell = (DataGridViewComboBoxCell)this._robotSelectionDataGridView[Common.SECOND_COLUMN, currentRow];
handToUseCell.Items.Clear();
foreach (Hand hand in robotInfo.Hands)
{
  handToUseCell.Items.Add(hand.Name.ToString());
}
handToUseCell.DisplayStyle = (handToUseCell.Items.Count > 1) ? DataGridViewComboBoxDisplayStyle.ComboBox :
                                                    DataGridViewComboBoxDisplayStyle.Nothing;
handToUseCell.Value = handToUseCell.Items[0];
handToUseCell.Style.BackColor = Color.DimGray;