是否可以在 DataGridView int 列中显示空字符串而不是 0? [英] Is it possible to display empty string instead of 0 in DataGridView int columns?
问题描述
我有一个数据表,其中包含有关音轨的信息.存储轨道编号的 DataTableColumn 是 UInt32 类型,因此当我在 DataGridView 中显示 DataTable 时,我可以按该列对数据进行排序.对于没有曲目编号的曲目,我在 DataTable 中得到了 0.
I have a DataTable filled with information about audio tracks. DataTableColumn that stores the track number is of a UInt32 type so when I display the DataTable in DataGridView, I'm able to sort data by that column. For tracks when there is no track number I've got 0 in DataTable.
data.Tables["active"].Columns["Track"].DataType = Type.GetType("System.UInt32");
是否可以将 DataGridView 中该列中的每个 0 显示为空字符串(无)?但是仍然将其存储为 UInt32 0 在 DataTable 中以便能够对曲目进行排序?
Is it possible to display every 0 in that column in DataGridView as an empty string (nothing)? But still have it stored as UInt32 0 in DataTable to be able to sort the tracks?
推荐答案
当然,您可以使用 CellFormatting
事件:
Sure, you can use the CellFormatting
event:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "Track")
{
uint value = (uint)e.Value;
if (value == 0)
{
e.Value = string.Empty;
e.FormattingApplied = true;
}
}
}
这篇关于是否可以在 DataGridView int 列中显示空字符串而不是 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!