DataGridView column format

余生颓废 提交于 2020-01-06 08:13:45

问题


There is my reading to DGV

public void ReadFromDB()
{
    dataset = DB.ReadFromDB("SELECT * FROM orders");
    dataGridView1.DataSource = dataset.Tables[0];

    //another columns....

    DataGridViewColumn sellDate = new DataGridViewTextBoxColumn();
    sellDate.DataPropertyName = "sell_date";
    sellDate.ReadOnly = true;
    sellDate.Name = "Дата продажи";
    dataGridView1.Columns.Add(sellDate);
    dataGridView1.Columns[4].DefaultCellStyle.Format = "dd/MM/yyyy";
    dataGridView1.Columns[4].Width = 80;
    dataGridView1.Update();
}

Why formatting doesn't work? DefaultCellStyle.Format is set:


回答1:


Make sure that the column index is [4] or [5]. You can check it from designer also.

Also make sure that the column is of DateTime type then only it will be get formatted.




回答2:


If your getting this from the database, its getting the format from your table.




回答3:


it may be the slash/backslash issue. Try this:

dataGridView1.Columns[4].DefaultCellStyle.Format = "dd\\/MM\\/yyyy";

or

dataGridView1.Columns[4].DefaultCellStyle.Format = @"dd\/MM\/yyyy";



回答4:


just in case if someone is looking for the answer to this question...

dataGridView1.Columns[4].DefaultCellStyle.Format = "dd'/'MM'/'yyyy";

all you have to do is add single quotations before and after the slash like this '/'



来源:https://stackoverflow.com/questions/17270212/datagridview-column-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!