C
C#2y ago
Cuda

❔ Format column DataGridview in WPF

I have the following epooch time data from a csv that I'm trying to import into a datagrid. What code can we use within the grid to convert it to regular Date Time format ie. 12/14/2022 3:00pm etc. ? 1671026261
5 Replies
Alexicon
Alexicon2y ago
I think you can get a datetime from an epoch value like this:
long epochSeconds = 1671026261;

DateTime myDateTime = DateTimeOffset.FromUnixTimeSeconds(epochSeconds).DateTime;
long epochSeconds = 1671026261;

DateTime myDateTime = DateTimeOffset.FromUnixTimeSeconds(epochSeconds).DateTime;
then to get it to display in your datagridview depends on how you are binding the data, which could be a myriad of ways so its hard to say without knowing but the easiest way would be to just expose a property with the conversion above and bind to that.
Cuda
CudaOP2y ago
Do you have a link where they do this approach? Not sure what I'd be looking for. 😄 I'm binding it with a datatable. It's autogenerating columns. How can we expose the property?
Alexicon
Alexicon2y ago
Ok in that case if you are binding directly to the datatable then you can create a converter to handle the datetime. For example given this xaml:
<DataGrid ItemsSource="{Binding MyTable}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header ="Name" Binding="{Binding Path=Name}"/>
<DataGridTextColumn Header ="DateTime" Binding="{Binding Path=DateTime, Converter={StaticResource xDataTimeColumnConverter}}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid ItemsSource="{Binding MyTable}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header ="Name" Binding="{Binding Path=Name}"/>
<DataGridTextColumn Header ="DateTime" Binding="{Binding Path=DateTime, Converter={StaticResource xDataTimeColumnConverter}}"/>
</DataGrid.Columns>
</DataGrid>
And this view model:
public class MainWindowViewModel
{
public MainWindowViewModel()
{
MyTable = new DataTable();

MyTable.Columns.Add("Name");
MyTable.Columns.Add("DateTime");

for (int row = 0; row < 5; row++)
{
DataRow myRow = MyTable.NewRow();
myRow["Name"] = $"Row: {row + 1}";
myRow["DateTime"] = DateTimeOffset.Now.ToUnixTimeSeconds();
MyTable.Rows.Add(myRow);

Thread.Sleep(1000);
}
}

public DataTable MyTable { get; }
}
public class MainWindowViewModel
{
public MainWindowViewModel()
{
MyTable = new DataTable();

MyTable.Columns.Add("Name");
MyTable.Columns.Add("DateTime");

for (int row = 0; row < 5; row++)
{
DataRow myRow = MyTable.NewRow();
myRow["Name"] = $"Row: {row + 1}";
myRow["DateTime"] = DateTimeOffset.Now.ToUnixTimeSeconds();
MyTable.Rows.Add(myRow);

Thread.Sleep(1000);
}
}

public DataTable MyTable { get; }
}
You could make this converter:
public class DataTimeColumnConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is string stringValue && long.TryParse(stringValue, out long epochSeconds))
{
return DateTimeOffset.FromUnixTimeSeconds(epochSeconds).DateTime.ToString();
}

return null;
}

public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class DataTimeColumnConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is string stringValue && long.TryParse(stringValue, out long epochSeconds))
{
return DateTimeOffset.FromUnixTimeSeconds(epochSeconds).DateTime.ToString();
}

return null;
}

public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Which would result in this table:
Name | DateTime
Row: 1 | 12/16/2022 12:54:19 AM
Row: 2 | 12/16/2022 12:54:19 AM
Row: 3 | 12/16/2022 12:54:19 AM
Row: 4 | 12/16/2022 12:54:19 AM
Row: 5 | 12/16/2022 12:54:19 AM
Name | DateTime
Row: 1 | 12/16/2022 12:54:19 AM
Row: 2 | 12/16/2022 12:54:19 AM
Row: 3 | 12/16/2022 12:54:19 AM
Row: 4 | 12/16/2022 12:54:19 AM
Row: 5 | 12/16/2022 12:54:19 AM
You can find a working example of this here: https://github.com/AlexLexicon/Discord.Example.DataGridviewEpoch
GitHub
GitHub - AlexLexicon/Discord.Example.DataGridviewEpoch
Contribute to AlexLexicon/Discord.Example.DataGridviewEpoch development by creating an account on GitHub.
Cuda
CudaOP2y ago
Great gonna check it out
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server