Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / January 2006

Tip: Looking for answers? Try searching our database.

DataColumn.Exression and string formatting

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
A.M-SG - 30 Jan 2006 00:11 GMT
Hi,

I am truing to add a calculated column to my data table. The column must
show something like: "Date: 2005-01-23"

I underestand that we can use Convert to convert a DateTime value to string.
How can I convert it in specific date format?

Thank you,
Alan


Elton W - 30 Jan 2006 00:44 GMT
Hi Alan,

Try

DateTimeObj.ToString("yyyy-MM-dd");

HTH

Elton Wang

> Hi,
>
[quoted text clipped - 8 lines]
>
>  
A.M-SG - 30 Jan 2006 03:03 GMT
It doesn't work.

The context is DataColumn.Expression syntax. What you said is C# syntax and
you cannot use it in DataColumn.Expression realm.

Regards,

Alan

> Hi Alan,
>
[quoted text clipped - 17 lines]
>> Thank you,
>> Alan
"Peter Huang" [MSFT] - 30 Jan 2006 06:56 GMT
Hi

Are you running a ASP.NET or winforms application?
Are you using a Datagrid?

Based on my research, here is some code about how to do that in a Grid view
in winform.
1.      Set current thread's date pattern to "yyyy-MM-dd"

  [STAThread]
       static void Main()
       {
           CultureInfo ci =
(CultureInfo)CultureInfo.CurrentCulture.Clone();
           ci.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
           Thread.CurrentThread.CurrentCulture = ci;
           Application.EnableVisualStyles();
           Application.SetCompatibleTextRenderingDefault(false);
           Application.Run(new Form1());
       }

2. For the string as "Date: xxxx-xx-xx"
       private void Form1_Load(object sender, EventArgs e)
       {
           DataTable table = new DataTable();
           //// Create the first column.
           DataColumn dateColumn = new DataColumn();
           dateColumn.DataType = System.Type.GetType("System.DateTime");
           dateColumn.ColumnName = "date";
           dateColumn.DefaultValue = "2002-4-18";

           //// Create the second, calculated, column.
           DataColumn testColumn = new DataColumn();
           testColumn.DataType = System.Type.GetType("System.String");
           testColumn.ColumnName = "Test";
           testColumn.Expression = "'Date: ' + SubString(Convert(date,
'System.String'),1,10)";
           
           // Add columns to DataTable.
           table.Columns.Add(dateColumn);
           table.Columns.Add(testColumn);

           DataRow row = table.NewRow();
           table.Rows.Add(row);
           DataView view = new DataView(table);
           this.dataGridView1.DataSource = view;
       }

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

A.M-SG - 30 Jan 2006 15:37 GMT
It is a winforms application. I like to set it up at the datatable level.
If it is not possible, then I have to do some custom code work.

In your example you changed the formatting behaviour globally. I cannot do
that.

Thank you for help.
Alan

> Hi
>
[quoted text clipped - 53 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
"Peter Huang" [MSFT] - 31 Jan 2006 06:55 GMT
Hi Alan,

The expression is ususally used to do the mathematical or string operation,
it did not have many string format feature.
In the whole application view, the datatable should be in the data layer,
the string format  should be taken care in the presentation layer.
e.g. the Winform DataGrid,GridView.......

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

W.G. Ryan - MVP - 30 Jan 2006 13:55 GMT
A.M:

Do you need it in the DataColumn as such or do you just need it displayed in
a grid or other control? You can add a dataGridTableStyle, then a
dataGridColumnStyle if you're using Winforms to do this in a grid. You can
just specify the formatter if it's a textbox (although in 2005 there's a
MaskedTextBox which can do this for you). You can do the same with a ASP.NET
datagrid, just specifying the formatter
http://www.stevex.org/CS/blogs/dottext/articles/158.aspx
> Hi,
>
[quoted text clipped - 6 lines]
> Thank you,
> Alan
A.M-SG - 30 Jan 2006 15:40 GMT
I am trying to do that at DataSet level. I know how to do formatting at
presentation level.

Thank you,
Alan

> A.M:
>
[quoted text clipped - 15 lines]
>> Thank you,
>> Alan
W.G. Ryan - MVP - 30 Jan 2006 18:27 GMT
Alan:

Just to make sure i understand the problem, is it that you're trying to
store a formatted String in a data column of type string?

> I am trying to do that at DataSet level. I know how to do formatting at
> presentation level.
[quoted text clipped - 21 lines]
>>> Thank you,
>>> Alan
A.M-SG - 31 Jan 2006 02:14 GMT
No, I don't want to store anything.

I want to use DataColumn.Expression to have a calculated column

The calculated column is string concatenation between 3 other
columnscincluding a date column. I like to format that column to '
yyyy/MM/dd'

Does that make any sense?

Regards,
Alan

> Alan:
>
[quoted text clipped - 26 lines]
>>>> Thank you,
>>>> Alan

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.