是否可以在DataGridView int列中显示空字符串而不是0? [英] Is it possible to display empty string instead of 0 in DataGridView int columns?
问题描述
我有一个DataTable,其中填充了有关音轨的信息.存储轨道号的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显示为空字符串(什么都没有)?但是仍然将其存储为DataTable中的UInt32 0以便能够对轨道进行排序吗?
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屋!