得到|在DataGridView中设置波斯日期。 [英] Get | Set Persian Date in DataGridView.

查看:94
本文介绍了得到|在DataGridView中设置波斯日期。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家。

我用VS2010编写Windows应用程序,绑定SQLServer 2008。

我使用DataSource Wizard来操作DataSet,BindingSource,TableAdapter ,BindaingNavigator和DataGridView。

我的数据库中有一个名为Sample的表。

其中一个已经提交的DateTime数据类型。字段的名称是日期。

我希望在获取数据库时以及设置数据库时转换数据。

转换是从datetime到persian datetime并反转。



我解决了获取DataGridView中的数据转换 CellFormatting此代码的事件:

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)
                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;
            }
        }





它的工作完美。

但问题是,当用户输入日期为波斯格式(例如:1391/12/21)时,这样就保存到数据库中,并且没有发生转换。



我知道如何将persianDate转换为DateTime,但不知道在哪里写它的代码,用户在DatagridView上输入了波斯日期值并自动转换为DateTime并保存到数据集或数据源或表格。



感谢您的帮助。



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.

推荐答案

使用数据库类型 DATE 而不是字符串,这样,它将从格式/文化中抽象出来。



剩下的问题将是使用正确的格式表示 DateTime 。请查看所有方法 ToStrings

http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ]。



注意格式可以由格式字符串,文化定义(通过实现 IFormatProvider ,请参阅此处的代码示例: http://msdn.microsoft.com/en-us/library/ht77y576.aspx [ ^ ])或两者。



格式字符串请请参阅:

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


这篇关于得到|在DataGridView中设置波斯日期。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆