2009-07-24 42 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数据将尝试将数据解析为单元格中的日期时间。如果它是有效的日期时间,那么它会将数据转换为我们应用的格式。