views:

522

answers:

2

hello,

I fill a datatable from using the fill method from a SqlDataAdapter. I then bind it to a DataGridView using the DataSource property

dataGridView1.DataSource = mydataTable;

I would like the DateTime columms to also show the seconds. what do I need to change to default to shoing the seconds.

Thanks

+2  A: 

For winforms DataGridView, you can use the Column DefaultCellStyle.Format to set the format string for the DateTime, like this:

public Form1()
{
    InitializeComponent();

    dataGridView1.DataSource = new List<MyObject>
    {
        new MyObject{ MyDateTime= DateTime.Now }
    };

    dataGridView1.Columns["MyDateTime"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
}

public class MyObject
{
    public DateTime MyDateTime { get; set; }
}
Andy White
Didn't work for me - the Columns[<columname>] was always null for Databound items.
endian
A: 

The design-time way of doing this is to change the DefaultCellStyle property on the relevant column. Click on the "..." button and then "Format" item in the resulting pop-up box can be used.

endian