问题
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:
- how to i really converting the column of date, not by display text
- formating the column of date
- formating the repository itemcombobox
- using the original format, but on repository itembox change the display text same as how i did on column date
- 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