4

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

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

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

4

2 回答 2

10

当然,您可以使用该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;
        }
    }
}
于 2013-05-25T19:43:52.090 回答
0

只是一个小建议...用“System.String”添加另一列并使其值等于 Track 列(隐藏轨道列),然后您可以在新的可见列中用空字符串替换 0

于 2013-05-25T19:43:27.100 回答