Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 894
Joudicek Jouda Avatar asked Dec 26 '22 03:12

Joudicek Jouda


1 Answers

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;
        }
    }
}
like image 91
Thomas Levesque Avatar answered Dec 31 '22 14:12

Thomas Levesque