-2

I want to convert string to date type in my wpf application. following is code.

Connection con1 = new Connection();
cmbStatementDate.Items.Clear();
con1.dataGet("SELECT distinct  statement_date from stockstatement ORDER BY statement_date asc");
DataTable dt1 = new DataTable();
con1.sda.Fill(dt1);
foreach (DataRow dr1 in dt1.Rows)
{
    cmbStatementDate.Items.Add(dr1["statement_date"].ToString());
}

How to convert its result into date format(DD-MM-YYYY) and saved into cmbStatementDate(combobox).

Thanks in Advance.

mjwills
  • 23,389
  • 6
  • 40
  • 63
  • 1
    Is the `statement_date` column in your database really a string type and not a real date? – DavidG Feb 21 '19 at 11:27
  • 1
    Dates have no format. They are binary values. *Remove* `.ToString()` altogether. Best case, `statement_date` is a text field and `ToString()` does nothing. Worst case, it converts a valid `DateTime` object into a string that needs to be parsed back into a `DateTime` – Panagiotis Kanavos Feb 21 '19 at 11:27
  • Please show us the `CREATE TABLE` script for `stockstatement`. – mjwills Feb 21 '19 at 11:29
  • Does https://stackoverflow.com/a/52124498/34092 work for you? – mjwills Feb 21 '19 at 11:30
  • What is the table's schema? What desktop stack are you using? WPF combos have a `StringFormat` attribute as mjwills pointed out – Panagiotis Kanavos Feb 21 '19 at 11:38

3 Answers3

0

I'll assume the field is a datetime or date field. If the field is varchar it should be parsed to datetime or date in the SQL query.

There's no need to convert dates to strings, or even add items one by one. You can use data binding to bind directly to the DataTable.

Windows Forms

Data binding combo boxesis described in How to: Bind a Windows Forms ComboBox or ListBox Control to Data

You can also use the FormatString property to specify how the control should display the dates, eg ::

//Use local short date format
cmbStatementDate.FormatString = "d";
//or hard-coded short format
//  cmbStatementDate.FormatString = "MM-dd-YYYY";
cmbStatementDate.DisplayMember = "statement_date";
cmbStatementDate.DataSource=dt;

The best place to specify the FormatString and DisplayMember is the form designer itself though, not the code-behind file. This makes it a lot easier to localization and display formats.

Data binding means there's no need to redraw the combobox each time a new item is added either. The entire combo is redrawn only after all data is loaded.

It's also possible to pull control properties from settings or resource files. This would allow someone to localize an application simply by creating a new settings or resources file.

In general, localization, globalization and data binding are baked into .NET since 2002. You can create an application just once and easily localize it for different markes without formatting strings by hand.

You can read about data binding in general in :

A bit of warning

Don't use the data binding order shown in the documentation :

cmbStatementDate.DataSource=dt;
cmbStatementDate.DisplayMember = "statement_date";

This would repaint the control twice if possible, using the original DisplayMember value.

WPF

Data binding and format strings work in WPF too, and are probably even easier to use. Without using data binding, one could create the formatted combobox with :

<ComboBox x:Name="cmbStatementDate" 
          DisplayMemberPath="statement_date" ItemStringFormat="d" />

or

<ComboBox x:Name="cmbStatementDate" 
          DisplayMemberPath="statement_date" ItemStringFormat="dd-MM-yyyy" />

And load the data with :

cmbStatementDate.ItemsSource=dt1;

This code looks like data binding and yet it isn't.

With data binding, the combo can bind to properties in the codebehind or a ViewModel class, eg :

<ComboBox x:Name="cmbStatementDate" 
          DisplayMemberPath="statement_date" 
          ItemStringFormat="dd-MM-yyyy" 
          ItemsSource="{Binding Path=MyDateData}"
          SelectedValue="{Binding Path=SelectedDate}" />

In this case MyDateData and SelectedDate refer to properties in the codebehind or ViewModel.

Data Binding in WPF is described in Data Binding Overview

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
-2

You can edit your code, like this

Connection con1 = new Connection();
cmbStatementDate.Items.Clear();
con1.dataGet("SELECT distinct  statement_date from stockstatement ORDER BY statement_date asc");
DataTable dt1 = new DataTable();
con1.sda.Fill(dt1);
foreach (DataRow dr1 in dt1.Rows)
{
     cmbStatementDate.Items.Add(Convert.ToDateTime(dr1["statement_date"]).ToString("DD-MM-YYYY"));
}
demo
  • 6,038
  • 19
  • 75
  • 149
Emre Ekşioğlu
  • 359
  • 2
  • 13
-3

Do you mean it is already a date\datetime type on the table and you want to see as a dd-MM-yyyy string? If so:

((DateTime)dr1["statement_date"]).ToString("dd-MM-yyyy");

would do it.

Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39