2009-07-24 46 views
1

我正在讀取Excel表格中的數據並將其顯示在數據gridview中。在Excel中有一些日期列。所以當我從Excel中讀取數據並將其綁定到dataGridView.The日期以「02/02/2009 12:00:00 AM」格式顯示,但excel列中的實際數據格式爲「2/2/2009」。因此,如何更改日期格式在datagridview中。無法在數據集列中格式化日期,GridView

,因爲我從數據集綁定數據我沒有任何模板列或綁定列設置,所​​以我不知道在哪裏設置的HTMLEncode =「假」 DataFormatString =「{0:T】」

是有沒有辦法做到這一點,請幫助我。

請找到下面的代碼示例。

string OleDbConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "+ FileUpload1.PostedFile.FileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""; 

string strSheetName = "Sheet1"; 
OleDbConnection oledbConnection; 
OleDbCommand oledbCommand; 
OleDbDataAdapter oledbAdapter; 

oledbCommand = new OleDbCommand(); 
oledbAdapter = new OleDbDataAdapter(); 
DataSet dsExcellData = new DataSet(); 

oledbConnection = new OleDbConnection(OleDbConnection); 
oledbConnection.Open(); 
oledbCommand.Connection = oledbConnection; 


oledbCommand.CommandText = "Select * from [" + strSheetName + "$]"; // i want to find this sheet name 
oledbAdapter.SelectCommand = oledbCommand; 
oledbAdapter.Fill(dsExcellData); 

oledbConnection.Close(); 

GridView1.DataSource = dsExcellData.Tables[0]; 

GridView1.DataBind(); 

=========================================== =============== 我試過

dsExcellData.Tables [0] .Rows [rowcount] [「date_column」]。ToString()] = dsExcellData.Tables [0 ] .Rows [行數] [ 「date_column」]的ToString()]的ToString( 「d」)。。

但該值沒有被賦值爲「mm/dd/yyyy」它也將時間默認時間再次設置爲(mm/dd/yyyy hh:mm:ss AM)。

============================================== ===============

我只是將數據集分配給gridview。問題是數據集正在讀取格式爲mm/dd/yyyy的日期列hh: mm:ss AM.I無法更改數據集中的數據。

============================================== ===============

Finaly我得到了斯科特的回答:

我們要在DataGridView的ItemDataBound添加下面的代碼:

protected void dgValidatedData_ItemDataBound1(object sender, DataGridItemEventArgs e) 
{ 

     for (int i = 0; i <= e.Item.Cells.Count - 1; i++) 
     { 
      System.DateTime cellDate = default(System.DateTime); 
      if (System.DateTime.TryParse(e.Item.Cells[i].Text, out cellDate)) 
      { 
       e.Item.Cells[i].Text = string.Format("{0:d}", cellDate); 
      } 
     } 

} 
+0

你會提前知道所有的列名,還是那個動態的? – ScottE 2009-07-24 12:09:06

+0

我會知道列名,我會從數據庫中獲取它。 – Jebli 2009-07-25 14:10:25

回答

2

好的,試試這個,其中「Item」是列名(可能是多個),這是需要格式化的日期。這當然是vb.net,但你可以將其排除。我相信有更好的方法,但這是有效的。

Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     For i As Integer = 0 To e.Row.Cells.Count - 1 
      If gv.HeaderRow.Cells(i).Text = "Item" Then 
       e.Row.Cells(i).Text = String.Format("{0:d}", CType(e.Row.Cells(i).Text, Date)) 
      End If 
     Next 
    End If 
End Sub 

或者,如果你不知道哪些列將有日期,下面的工作還有:

Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     For i As Integer = 0 To e.Row.Cells.Count - 1 
      Dim cellDate As Date 
      If Date.TryParse(e.Row.Cells(i).Text, cellDate) Then 
       e.Row.Cells(i).Text = String.Format("{0:d}", cellDate) 
      End If 
     Next 
    End If 
End Sub 
+0

嗨,你是代碼工作。我正在使用GridView和請找在C#代碼中的man.Thanks: 保護無效dgValidatedData_ItemDataBound1(對象發件人,DataGridItemEventArgs E) { 的for(int i = 0;我<= e.Item.Cells.Count - 1; i ++) System.DateTime cellDate = default(System.DateTime); (System.DateTime.TryParse(e.Item.Cells [i] .Text,out cellDate)) e.Item.Cells [i] .Text = string.Format(「{0:d}」, cellDate); } } } } 謝謝soo – Jebli 2009-07-24 13:22:24

0

您應該在GridView列中定義的日期格式。例如:


<asp:GridView> 
... 
<asp:TemplateField> 
<ItemTemplate> 
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Date", "{0:T}") %>'></asp:Label> 
    </ItemTemplate> 
... 
</asp:GridView> 
+0

我沒有使用任何項目模板。因爲我動態綁定數據..我會知道列名稱,我將從數據庫檢索列名。感謝您的帖子。 – Jebli 2009-07-24 11:16:33

2

如果您將它綁定爲asp:BoundField,則需要將htmlencode設置爲false。

<asp:BoundField HtmlEncode="false" DataField="Blah" DataFormatString="{0:d}" /> 
+0

我沒有使用asp綁定字段。我只是將數據綁定到數據集中的數據網格。感謝您的答覆。 – Jebli 2009-07-24 11:27:28

0

下面是使用從GridView控件派生的自定義控制的解決方案:

using System; 
using System.Collections; 
using System.Web.UI.WebControls; 

namespace CustomControls { 

    public class FormattedGridView : GridView { 

    protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource) { 

     // Call base method and return the collection as an ArrayList 
     var columns = (ArrayList) base.CreateColumns(dataSource, useDataSource); 

     for (var i = 0; i < columns.Count; i++) { 

     var agf = columns[i] as AutoGeneratedField; 

     if (agf != null && agf.DataType == typeof(DateTime)) { 

      // create a new column because the AutoGeneratedField does not support 
      // the modification of the DataFormatString property 
      var bf = new BoundField(); 

      // copy some of the original properties 
      bf.DataField = agf.DataField; 
      bf.HeaderText = agf.HeaderText; 
      bf.HtmlEncode = false; 

      // set the format for the DateTime types 
      bf.DataFormatString = "{0:T}"; 

      // replace the existing auto-generated colums 
      columns[i] = bf; 
     } 

     } 

     return columns; 
    } 
    } 

} 
0

Finaly我得到了斯科特的回答:

我們必須在項目添加下面的代碼在DataGridView的數據綁定:

保護無效dg_ItemDataBound1(對象發件人,DataGridItemEventArgs E) {

for (int i = 0; i <= e.Item.Cells.Count - 1; i++) 
    { 
     System.DateTime cellDate = default(System.DateTime); 
     if (System.DateTime.TryParse(e.Item.Cells[i].Text, out cellDate)) 
     { 
      e.Item.Cells[i].Text = string.Format("{0:d}", cellDate); 
     } 
    } 

}

但上面的代碼將檢查所有綁定到datagrig.It數據將嘗試將數據解析爲單元格中的日期時間。如果它是有效的日期時間,那麼它會將數據轉換爲我們應用的格式。