How do I change the apperance of datetimes in a datagrid?
-
Hi, I am using a datagrid as the main display of my application. The datagrid is bound to a dataview. This dataview is constructed from a DataTable. The first column in the DataTable is of type DateTime. It is important for me that this column is of type DateTime since I want to be able to sort with respect to time, whatever time format is used. My problem is that I want the user to be able to customize the format in which the time is displayed in the first column. Any one who knows how I can accomplish this? I now about the DateTimeFormatInfo objects but I don't know where in the process to use them. Thanks in advance for any help. Rickard
-
Hi, I am using a datagrid as the main display of my application. The datagrid is bound to a dataview. This dataview is constructed from a DataTable. The first column in the DataTable is of type DateTime. It is important for me that this column is of type DateTime since I want to be able to sort with respect to time, whatever time format is used. My problem is that I want the user to be able to customize the format in which the time is displayed in the first column. Any one who knows how I can accomplish this? I now about the DateTimeFormatInfo objects but I don't know where in the process to use them. Thanks in advance for any help. Rickard
I haven't done it myself but I think it will works, if you use AutoGenerate Columns I think you can do it in
ItemDataBound
event, if no and you do not use AutoGenerate , in a Bound Coumn property builder there is Data Formatting Expression which I think if you play with it , it will do the works. Mazy "A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope -
Hi, I am using a datagrid as the main display of my application. The datagrid is bound to a dataview. This dataview is constructed from a DataTable. The first column in the DataTable is of type DateTime. It is important for me that this column is of type DateTime since I want to be able to sort with respect to time, whatever time format is used. My problem is that I want the user to be able to customize the format in which the time is displayed in the first column. Any one who knows how I can accomplish this? I now about the DateTimeFormatInfo objects but I don't know where in the process to use them. Thanks in advance for any help. Rickard
Like Mazdak said, you can change the display of the DateTime object when it is databound to the DataGrid. Example ASPX:
<asp:DataGrid runat="server" id="MyDataGrid" AutoGenerateColumns="false">
<columns>
<asp:TemplateColumn HeaderText="Date/Time">
<ItemTemplate>
<asp:Label ID="lblDate" Text='<%# DataBinder.Eval(Container.DataItem, "MyDate") %>' runat="server" OnDataBinding="lblDate_DataBinding" />
<ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:DataGrid>In the code behind:
private void lblDate_DataBinding(object sender, System.EventArgs e) {
Label lblDateTime = (Label)sender;
DateTime dteDateTime = System.Convert.ToDateTime(lblDateTime.Text);
lblDateTime.Text = dteDateTime.ToString("MMM dd YYYY");
}If you want to let the user choose the format I guess you could put DropDownList on the page with available formats and whenever that changes grab the format string to use it in the ToString method of the DateTime object. Should work. (?) Let me know.
Wally Atkins
Newport News, VA, USA
http://wallyatkins.com