[英]Default value always display blank when bind a datasource to a DataGridViewComboBoxColumn
[英]How to display text in DataGridViewComboBoxColumn when the databound value is null
我有一個帶有DataGridViewComboBoxColumn的數據綁定DataGridView。 如果組合框值為null,我想顯示文本。 我不想將空項添加到數據綁定列表中,因為我需要在每個datagridview行中顯示不同的文本。 如何使用默認的datagridview控件實現此目的?
您可以使用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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.