简体   繁体   中英

C# wpf DataGrid Binding Date (convert to Persian Format)

I have a DataGrid View which is binding to the database and gets info from the MySql database like this.

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;

And I add the data from the Database into my DataGrid View like this.

<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>

My problem is that my database column called register_date is in Miladi Date (2021/11/09) and I want to change it to Persian (Shamsi) date (18/08/1400).

I find this solution, since in database all dates are UTC but my clients want to see and insert Persian dates, i use this code for my Datagridview:

 <DataGridTextColumn Binding="{Binding Path=created_at, StringFormat=yyyy/mm/dd, ConverterCulture=fa-IR}" Header="تاریخ پرداخت" Width="150" IsReadOnly="True" />

and I and for inserting Persian date as UTC date in database I use this:

var value = PersianDate.Text;
        // Convert to Miladi
        DateTime dt = DateTime.Parse(value, new CultureInfo("fa-IR"));
        // Get Utc Date
        var dt_utc = dt.ToUniversalTime();

which change my PersianDate textbox into UTC date so i can insert into database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM