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

6
  • please don't concatenate string using sql use parameters only see stackoverflow.com/questions/11070434/… Commented Nov 9, 2021 at 13:46
  • Use a Binding Converter. Commented Nov 9, 2021 at 14:47
  • @nbk I don't get what does it have to do with my question. Commented Nov 10, 2021 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 stackoverflow.com/questions/30683664/… also mysql save dataes in 2021-01-01 keep that so, converting in mysql is costly Commented Nov 10, 2021 at 13:44
  • Does this answer your question? How convert Gregorian date to Persian date? Commented Nov 10, 2021 at 13:45

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.