Plazza Sele
Plazza Sele

Reputation: 319

Show only Date in DataGridView Column

I have a field as DateTime, I want to display only date( without time ) in my datagridview c#. I get the datetime. example: 22.03.2016 00:00:00 Any help would be appreciated. I have tried the following:

private void bindListToGridView1(List<Item> list)
{
    try
    {
        dataGridView1.AllowUserToAddRows = false;
        ListtoDataTableConverter converter = new ListtoDataTableConverter();
        DataTable dt2 = converter.ToDataTable(listToPresent);
        dataGridView1.DataSource = null;

        using (dt2)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "id";
            dataGridView1.Columns[0].HeaderText = "ID";
            dataGridView1.Columns[0].DataPropertyName = "id";
            dataGridView1.Columns[1].Name = "itemName";
            dataGridView1.Columns[1].HeaderText = "Item Name";
            dataGridView1.Columns[1].DataPropertyName = "itemName";
            dataGridView1.Columns[2].Name = "productionDate";
            dataGridView1.Columns[2].HeaderText = "Production Date";
            dataGridView1.Columns[2].DataPropertyName = "productionDate";
            dataGridView1.Columns[2].DefaultCellStyle.Format = "yyyy'/'MM'/'dd";
            dataGridView1.DataSource = dt2;
        }
    }
    catch (Exception ex)
    {
    }
}

Edit

Here is the method I use to create the DataTable:

public class ListtoDataTableConverter
{
    public DataTable ToDataTable<T>(List<T> items)
    {
        DataTable dataTable = new DataTable(typeof(T).Name);
        //Get all the properties
        PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (PropertyInfo prop in Props)
        {
            //Setting column names as Property names
            dataTable.Columns.Add(prop.Name);
        }
         foreach (T item in items)
        {
            var values = new object[Props.Length];
            for (int i = 0; i < Props.Length; i++)
            {
                //inserting property values to datatable rows
                values[i] = Props[i].GetValue(item, null);
            }
            dataTable.Rows.Add(values);
        }
         //put a breakpoint here and check datatable
        return dataTable;
    }
}

Upvotes: 0

Views: 4082

Answers (2)

Buğra
Buğra

Reputation: 23

There is an easier way. Next time, if you add data to DataGridView via clicking the little triangle at the right-top corner of DataGridView;

Edit your XXXDataSet.Designer according to:

this.columnYYY = new global::System.Data.DataColumn("YYY", typeof(**string**), null, global::System.Data.MappingType.Element);

Change String to DateTime. Problem solved enjoy. :)

Upvotes: 0

Reza Aghaei
Reza Aghaei

Reputation: 125197

The problem is your column is not of type DateTime so when you apply formatting, it doesn't work.

Take a look at the method that creates the data table, you created the columns using dataTable.Columns.Add(prop.Name); which it means the DataType of column is string and formatting will not apply on column. (As I guessed previously)

Note

  • You don't need to convert a List to a DataTable if you want to show it in DataGridView, it's enough to set it as DataSource of the grid.
  • To format a DateTime column to show only Date part, it's enough to set DefaultCellStyle.Format of that column to yyyy/MM/dd.
  • As a quick fix in your method, you can replace this line of code dataTable.Columns.Add(prop.Name); with this line of code: dataTable.Columns.Add(prop.Name, prop.PropertyType); which uses data type of property as data type of column.

Upvotes: 3

Related Questions