2013-07-21 46 views
1

enter image description here如何更改gridview中日期的格式?

我想更改日期列在我的GridView控件,以便它是DD-MM-YYYY,然後在時間結束時的00:00:00

格式的空間列的名稱是thedate和列數爲2 我試過dataGridView1[2]["thedate"].ToString("dd/MM/yyyy");

String str = dataGridView1[2]["thedate"].ToString("dd.MM.yyyy"); 

DateTime dt = DateTime.Now; 
     String formated = dataGridView1.Columns[2].ToString("dd.MM.yyyy"); 


String formated = String.Format("{0:dd.MM.yyyy}", dataGridView1.Columns[2]); 

和他們沒有工作,我不知道在哪裏可以從這裏走!感謝

+0

嘗試這樣做,在CellFormatting事件.. – matzone

回答

0

我認爲這個問題是

dataGridView1[2]["thedate"].ToString("dd.MM.yyyy"); 

此代碼是不是一個DateTime實例。

首先轉換到dataGridView1[2]["thedate"]然後DateTime上使用ToString("dd.MM.yyyy");

。這將工作。

+0

感謝您的快速回復達斯:)我會仰望如何,現在再次感謝呢! – user2058186

+0

我是你的父親! – DarthVader

+0

哈哈,只要我能得到這個工作:p查找如何使用datetime now – user2058186

0

嘗試類似:

DateTime dt = new DateTime(); 
dt = Datatime.TryParse(dataGridView[2]["thedate"].ToString()); 
String dtFormat = dt.Tostring("dd.MM.yyyy"); 

應該有所幫助。

+0

非常感謝回覆malcor :)底線似乎正在工作,但頂端行有幾個錯誤,方法使用像一個類型和沒有過載方法:/ http://gyazo.com/85b59af8d0eeb316a834e99d652ba2c7.png – user2058186

+0

失去嘗試從TryParse –

+0

在那裏我已經修復它。您可以在嘗試解析之前初始化一個DT的新實例,或者您可以從之前的版本中刪除'new關鍵字,並立即使用該數據進行處理。 – Malcor

0

,我認爲這會工作:

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