-2

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

Arsalan Ahmadi
  • 55
  • 1
  • 10
  • please don't concatenate string using sql use parameters only see https://stackoverflow.com/questions/11070434/using-prepared-statement-in-c-sharp-with-mysql – nbk Nov 09 '21 at 13:46
  • Use a [Binding Converter](https://learn.microsoft.com/en-us/dotnet/api/system.windows.data.binding.converter?view=windowsdesktop-5.0). – Clemens Nov 09 '21 at 14:47
  • @nbk I don't get what does it have to do with my question. – Arsalan Ahmadi Nov 10 '21 at 13:19
  • 1
    @ArsalanAhmadi this is a comment to giove you some information that is vital in your programm. furthwer a short search find https://stackoverflow.com/questions/30683664/how-convert-gregorian-date-to-persian-date also mysql save dataes in 2021-01-01 keep that so, converting in mysql is costly – nbk Nov 10 '21 at 13:44
  • Does this answer your question? [How convert Gregorian date to Persian date?](https://stackoverflow.com/questions/30683664/how-convert-gregorian-date-to-persian-date) – nbk Nov 10 '21 at 13:45
  • @nbk thanks for helping I use this method for viewing (changing dates to persian date while binding): DataGridTextColumn Binding="{Binding Path=created_at, StringFormat=yyyy/mm/dd, ConverterCulture=fa-IR}" Header="date" Width="150" IsReadOnly="True" /> and for saving, changing persian date to UTC i use this: var value = PersianDate.Text; DateTime dt = DateTime.Parse(value, new CultureInfo("fa-IR")); var dt_utc = dt.ToUniversalTime(); – Arsalan Ahmadi Nov 12 '21 at 11:08

1 Answers1

0

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.

Arsalan Ahmadi
  • 55
  • 1
  • 10