我有一個 DataGrid 視圖,它系結到資料庫并像這樣從 MySql 資料庫中獲取資訊。
MySqlCommand cmd3 = new MySqlCommand("SELECT * FROM owner_units WHERE unit_id =" UNITID, conn);
MySqlDataAdapter adp2 = new MySqlDataAdapter(cmd3);
DataSet ds2 = new DataSet();
adp2.Fill(ds2, "LoadDataBinding2");
owner_unit.DataContext = ds2;
我像這樣將資料庫中的資料添加到我的 DataGrid 視圖中。
<DataGrid HorizontalAlignment="Left" Name="owner_unit" ItemsSource="{Binding LoadDataBinding2}" AutoGenerateColumns="False" IsReadOnly="True" CanUserAddRows="False" Height="99" Margin="36,463,0,0" VerticalAlignment="Top" Width="420">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding id}" Header="OwnerID" Width="100" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding percent}" Header="Percentage" Width="100" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding register_date}" Header="sell date " Width="200" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
我的問題是我的名為 register_date 的資料庫列在 Miladi 日期 (2021/11/09) 中,我想將其更改為波斯語 (Shamsi) 日期 (18/08/1400)。
uj5u.com熱心網友回復:
我找到了這個解決方案,因為在資料庫中所有日期都是 UTC 但我的客戶想要查看和插入波斯日期,我將此代碼用于我的 Datagridview:
<DataGridTextColumn Binding="{Binding Path=created_at, StringFormat=yyyy/mm/dd, ConverterCulture=fa-IR}" Header="????? ??????" Width="150" IsReadOnly="True" />
并且我為了在資料庫中插入波斯日期作為UTC日期我使用這個:
var value = PersianDate.Text;
// Convert to Miladi
DateTime dt = DateTime.Parse(value, new CultureInfo("fa-IR"));
// Get Utc Date
var dt_utc = dt.ToUniversalTime();
它將我的 PersianDate 文本框更改為 UTC 日期,以便我可以插入到資料庫中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/357934.html
上一篇:Dispatcher.BeginInvoke(new...ProgressBarProcess.Value=1沒有顯示?
