Search code examples
wpfxamlstring-formattingwpfdatagrid

StringFormat file size in datagrid column


I want to display in a datagrid column size of a directory but I don't know how to format the string with "StringFormat" in XAML to display KB , MB or GB and keep the ability to sort by size .

Thanks


Solution

  • You can have an additional property in ItemsSource of the data grid. Populate that new property with the size of directory keeping unit a constant. So let say you have a directory with 100 KB and another with 1 MB. So convert 1 MB into equivalent KB and populate in the newly added property. Do not show this property on Datagrid just use it in sort member path. and keep the string property in View which would show 100 KB or 1 MB values. To better understand what I am trying to say, please refer this SO post

    Let me know if things are not clear to you !!