PJW
PJW

Reputation: 5397

C# Only want to display Time in Datatable (not Date)

The following code creates a datatable which is then used as the datasource for a datagridview.

 public void PopulateTimesheet()
    {
        DataTable dT = new DataTable();

        dT.Columns.Add("Case No", typeof(string));
        dT.Columns.Add("Charge Code", typeof(string));
        dT.Columns.Add("Start Time", typeof(DateTime));            
        dT.Columns.Add("End Time", typeof(DateTime));
        dT.Columns.Add("Units", typeof(int));
        dataTimeSheet.DataSource = dT;
    }

However the two datetime fields obviously display the whole date & time i.e. 18/01/2012 08:23 and I just want to show the time 08:23 (preferrably in 24 hour mode). I can't however find a way to do this. Also is there a way to add a mask of the same format to ensure users input times in the correct format.

Thanking everyone in anticipation of their assistance.

Upvotes: 1

Views: 6710

Answers (2)

Marius
Marius

Reputation: 1

My answer, for my works

this.dataGridView.Columns[3].DefaultCellStyle.Format = "HH:mm:ss";

[3] -is my column for set hour , first column[0] is index

Upvotes: 0

Andras Zoltan
Andras Zoltan

Reputation: 42333

Please see this MSDN topic that tells you exactly what to do: http://msdn.microsoft.com/en-us/library/f9x2790s.aspx

dataGridView1.Columns["Start Time"].DefaultCellStyle.Format = "HH:mm:ss";

Assuming your DataGridView is called dataGridView1.

As for the input mask - some more googling tells me that you have to create your own cell type for that. I found this codeproject article detailing one ready-rolled solution: http://www.codeproject.com/KB/grid/DataGridViewMaskedText.aspx.

Upvotes: 8

Related Questions