0
我有一个DataGridView DataGridView与DataGridViewComboBoxColumn。如果组合框值为空,我想显示一个文本。我不想将一个空项添加到数据绑定列表中,因为我需要在每个datagridview行中显示不同的文本。 我将如何使用默认的datagridview控件实现此目的?如何在DataBound值为空时在DataGridViewComboBoxColumn中显示文本
我有一个DataGridView DataGridView与DataGridViewComboBoxColumn。如果组合框值为空,我想显示一个文本。我不想将一个空项添加到数据绑定列表中,因为我需要在每个datagridview行中显示不同的文本。 我将如何使用默认的datagridview控件实现此目的?如何在DataBound值为空时在DataGridViewComboBoxColumn中显示文本
可以使用CellFormatting事件来改变任何显示的值:
//attach in code or via designer:
dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
//example implementation:
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == Column1.Index && e.Value==null)//where Column1 is your combobox column
{
e.Value = "Empty";
e.FormattingApplied = true;
}
}
如果我的价值改变的东西,是不是在数据绑定列表,然后我得到一个错误。 – Daniel
有没有其他想法?问题是DataGridViewComboBoxCell没有文本属性。该值是数据绑定的,因此无法为其分配文本。 – Daniel
如果e.FormattingApplied = true,你可以试试吗?设置? (也编辑原帖以便添加) –