Search code examples
wpfvb.netxamldatagridwpfdatagrid

XAML datagrid column format duration grater than 24 hours


I have a datagrid that gets data from a MySQL database and one column represents the total time like 4.22:15:00 (4 days, 22 hours, 15 minutes, 00 seconds)

But I want the output to be 118:15:00 (118 hours, 15 minutes, 00 seconds) or 118:15 is also okay.

I'm using this code for that datagrid column:

<DataGridTextColumn Header="HOURS" Binding="{Binding Hours}" />

Which results in:

enter image description here

I have tried many different possibilities for the StringFormat property like:

StringFormat={}{0:hh':'mm}

but I couldn't get the desired output...

For the code behind I'm using VB.


Solution

  • Its just about parsing the string in an IValueConverter:

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string str = value as string;
            int idot = str.IndexOf(".");
            int days = int.Parse(str.Substring(0,idot ));
            int idotdot = str.IndexOf(":"); 
            int ho = int.Parse(str.Substring(idot+1, idotdot-idot-1));
            int newho = days * ho; 
            string res = newho.ToString() + str.Substring(idotdot);
            return res;
        }
    

    You might need to handle the exceptions.