|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DataColumn.Exression and string formattingHi,
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 Hi Alan,
Try DateTimeObj.ToString("yyyy-MM-dd"); HTH Elton Wang Show quote "A.M-SG" wrote: > 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 > > > > > 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 Show quote "Elton W" <Elt***@discussions.microsoft.com> wrote in message news:349E693A-A9E5-4181-B906-2DA15E740A2E@microsoft.com... > Hi Alan, > > Try > > DateTimeObj.ToString("yyyy-MM-dd"); > > HTH > > Elton Wang > > "A.M-SG" wrote: > >> 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 >> >> >> >> >> 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 Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights. 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 Show quote ""Peter Huang" [MSFT]" <v-phu***@online.microsoft.com> wrote in message news:XJO6WpWJGHA.224@TK2MSFTNGXA02.phx.gbl... > 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 > > Get Secure! - www.microsoft.com/security > This posting is provided "AS IS" with no warranties, and confers no > rights. > 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 Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights. 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 Show quote "A.M-SG" <alanalan@newsgroup.nospam> wrote in message news:%23T2qzGTJGHA.668@TK2MSFTNGP11.phx.gbl... > 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 > > > > I am trying to do that at DataSet level. I know how to do formatting at
presentation level. Thank you, Alan Show quote "W.G. Ryan - MVP" <WilliamRyan@nospam.gmail.com> wrote in message news:uaq$RSaJGHA.3176@TK2MSFTNGP12.phx.gbl... > 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 > "A.M-SG" <alanalan@newsgroup.nospam> wrote in message > news:%23T2qzGTJGHA.668@TK2MSFTNGP11.phx.gbl... >> 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 >> >> >> >> > > 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? Show quote "A.M-SG" <alanalan@newsgroup.nospam> wrote in message news:u%23Dp2NbJGHA.740@TK2MSFTNGP12.phx.gbl... > > I am trying to do that at DataSet level. I know how to do formatting at > presentation level. > > Thank you, > Alan > > > > "W.G. Ryan - MVP" <WilliamRyan@nospam.gmail.com> wrote in message > news:uaq$RSaJGHA.3176@TK2MSFTNGP12.phx.gbl... >> 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 >> "A.M-SG" <alanalan@newsgroup.nospam> wrote in message >> news:%23T2qzGTJGHA.668@TK2MSFTNGP11.phx.gbl... >>> 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 >>> >>> >>> >>> >> >> > > 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 Show quote "W.G. Ryan - MVP" <WilliamRyan@nospam.gmail.com> wrote in message news:uUUGhqcJGHA.3224@TK2MSFTNGP09.phx.gbl... > 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? > "A.M-SG" <alanalan@newsgroup.nospam> wrote in message > news:u%23Dp2NbJGHA.740@TK2MSFTNGP12.phx.gbl... >> >> I am trying to do that at DataSet level. I know how to do formatting at >> presentation level. >> >> Thank you, >> Alan >> >> >> >> "W.G. Ryan - MVP" <WilliamRyan@nospam.gmail.com> wrote in message >> news:uaq$RSaJGHA.3176@TK2MSFTNGP12.phx.gbl... >>> 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 >>> "A.M-SG" <alanalan@newsgroup.nospam> wrote in message >>> news:%23T2qzGTJGHA.668@TK2MSFTNGP11.phx.gbl... >>>> 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 >>>> >>>> >>>> >>>> >>> >>> >> >> > > |
|||||||||||||||||||||||