David ChopperField David ChopperField - 3 months ago 35
C# Question

how to set formatting date time on repositoryitem combobox?

how do i get the format time in my gridview column to set it on my repository ?
the date in database was using

date time
which return an output of
8/1/2016 12:00:00 AM

and i set in a gridcolumn with this method

private void arrange()
{

gridView1.Columns["date"].VisibleIndex = 1;
gridView1.Columns["date"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
gridView1.Columns["date"].DisplayFormat.FormatString = "dd MMM yyyy";
gridView1.Columns["date"].format.FormatString = "dd MMM yyyy";

gridView1.Columns["date"].Width = 150;
}


the display on the gridcolumn of date is

enter image description here

the problem is when i using repository item on filter row it still getting the date format from database which is
8/1/2016 12:00:00 AM
. the image was like this

enter image description here

the code to add item into repository combobox was

RepositoryItemComboBox repositoryItemComboBox2 = new RepositoryItemComboBox();

private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
GridView view = sender as GridView;
for (int i = 0; i < gridView1.RowCount; i++)
{
if (gridView1.GetDataRow(i) == null)
{
break;
}
string txt = gridView1.GetDataRow(i)["date"].ToString();
if (!repositoryItemComboBox2.Items.Contains(txt))
{
repositoryItemComboBox2.Items.Add(txt);
}
}

if (e.Column.FieldName == "date" && view.IsFilterRow(e.RowHandle))
{ e.RepositoryItem = repositoryItemComboBox2;

repositoryItemComboBox2.Sorted = true;

}
}


iam thinking , how to resolve this is:


  1. how to i really converting the column of date, not by display text

  2. formating the column of date

  3. formating the repository itemcombobox

  4. using the original format, but on repository itembox change the display text same as how i did on column date

  5. your suggestion



n.b: iam using devexpress

Answer

Change

string txt = gridView1.GetDataRow(i)["date"].ToString();

To

string txt = Convert.ToDateTime(gridView1.GetDataRow(i)["date"].ToString()).Date.ToString("dd MMM yyyy");

My tested code...

List<String> str = new List<String>();
str.Add("2016-08-01 12:00:00 AM");
str.Add("2016-08-02 12:00:00 AM");
str.Add("2016-08-03 12:00:00 AM");
str.Add("2016-08-04 12:00:00 AM");
str.Add("2016-08-05 12:00:00 AM");
str.Add("2016-08-06 12:00:00 AM");
str.Add("2016-08-07 12:00:00 AM");
str.Add("2016-08-08 12:00:00 AM");
str.Add("2016-08-09 12:00:00 AM");


foreach (var item in str)
{
  repositoryItemComboBox1.Items.Add(Convert.ToDateTime(item).Date.ToString("dd MMM yyyy"));
}

enter image description here

Changed column to DateTime, according to your resquest. Still work!

enter image description here