我有一个数据表,其中包含有关音轨的信息。存储轨道编号的 DataTableColumn 是 UInt32 类型,因此当我在 DataGridView 中显示 DataTable 时,我可以按该列对数据进行排序。对于没有轨道编号的轨道,我在 DataTable 中得到了 0。

data.Tables["active"].Columns["Track"].DataType = Type.GetType("System.UInt32");

是否可以将 DataGridView 中该列中的每个 0 显示为空字符串(无)?但是仍然将它存储为 DataTable 中的 UInt32 0 以便能够对轨道进行排序?

最佳答案

当然,您可以使用 CellFormatting 事件:

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;
        }
    }
}

关于c# - 是否可以在 DataGridView int 列中显示空字符串而不是 0?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16753230/

10-14 18:15
查看更多