Click here to Skip to main content
15,946,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Experts.
I write windows application with "VS2010" that bind "SQLServer 2008".
I use "DataSource Wizard" to manipulate DataSet,BindingSource,TableAdapter,BindaingNavigator and also DataGridView.
I have a "Table" named Sample in my "Database".
A one of it's filed has "DateTime" datatype. the field's name is "Date".
I want to "Convert" data when "get" from database and also when "set" to database.
The convertion is from "datetime" to "persian datetime" and reverse.

I solve "getting" data convertion in DataGridView with "CellFormatting" event by this code:
private void StudentExamDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            if (e.Value == DBNull.Value || e.Value == null)

            if (e.ColumnIndex < 0 || e.RowIndex < 0)
            if (e.ColumnIndex == StudentExamDataGridView.Columns["Date"].Index)
                e.FormattingApplied = true;
                e.Value = new PersianCalendarEx(Convert.ToDateTime(e.Value)).PersianDate;

it's work perfectly.
but problem is , when user input date in "persian format" (for example : 1391/12/21) this saved to database like that and no convertion occure.

I know how to convert persianDate to DateTime, but don't know where write it's code, that user entered persian date value on DatagridView and automatically convert to DateTime and save to dataset or datasource or table.

thanks for any help.
Updated 27-Jan-23 2:02am

1 solution

Use database type DATE instead of string, this way, it will be abstracted from format/culture.

The remaining problem will be using proper format in representing DateTime in your UI. Please see all the methods ToStrings:[^].

Pay attention that the format can be defined by format string, culture (via implemented IFormatProvider, see the code sample here:[^]), or both.

For format string, please see:[^],[^],[^].

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900