Is it possible to display empty string instead of 0 in DataGridView int columns?

柔情痞子 提交于 2021-02-16 08:47:04

问题


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");

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?


回答1:


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



回答2:


just a small suggestion ... add another column with "System.String" and make it's value equal to Track column (hide track column) and then you can replace 0 with empty string in the new visible column



来源:https://stackoverflow.com/questions/16753230/is-it-possible-to-display-empty-string-instead-of-0-in-datagridview-int-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!