Click here to Skip to main content
15,881,248 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:
C#
private void StudentExamDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value == DBNull.Value || e.Value == null)
                return;

            if (e.ColumnIndex < 0 || e.RowIndex < 0)
                return;
            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.
Posted
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:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

Pay attention that the format can be defined by format string, culture (via implemented IFormatProvider, see the code sample here: http://msdn.microsoft.com/en-us/library/ht77y576.aspx[^]), or both.

For format string, please see:
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx[^],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

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