how to set formatting date time on repositoryitem combobox?

为君一笑 提交于 2019-12-12 01:19:42

问题


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

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

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


回答1:


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




回答2:


Change Display Format and Edit Format of repositoryItem

 repositoryItemComboBox1.DisplayFormat.FormatString = "dd MMM yyyy";
 repositoryItemComboBox1.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
 repositoryItemComboBox1.EditFormat.FormatString = "dd MM yyyy";
 repositoryItemComboBox1.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;


来源:https://stackoverflow.com/questions/39157400/how-to-set-formatting-date-time-on-repositoryitem-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!