无法在数据集列GridView中格式化date

我正在读取Excel工作表中的数据,并将其显示在数据gridview中。在excel中有一些date列,所以当我从excel中读取数据并将其绑定到dataGridView时。date显示格式为“02 / 02/2009 12:00:00 AM“,但excel列中的实际数据格式为”2/2/2009“。因此如何在datagridview中更改date格式。

因为我绑定数据集的数据我没有任何模板列或绑定列设置,所​​以我不知道在哪里设置HtmlEncode =“False”DataFormatString =“{0:T}”

有没有办法做到这一点,请帮助我。

请find下面的代码示例。

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(); 

================================================== ========我试过了

()函数返回一个数据types的数据,这个数据types包含一个数据types的数据types,这个数据types包含一个数据types,一个数据types,一个数据types,一个数据types,一个数据types,一个数据types。 ;

但是这个值没有被赋值为“mm / dd / yyyy”,这也是重新设置时间的默认时间(mm / dd / yyyy hh:mm:ss AM)。

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

我只是将数据集分配给gridview。问题是数据集正在读取格式为mm / dd / yyyy hh:mm:ss AM的date列。我也无法更改数据集中的数据。

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

最后我从ScottE得到了答案:

我们必须在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); } } } 

好的,试试这个,其中“Item”是列名(可能是多个),这是一个需要格式化的date。 这当然是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 

或者,如果您不知道哪些列会有date,那么以下内容也将起作用:

 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 

如果你将它绑定为一个asp:BoundField,你需要将htmlencode设置为false。

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

您应该在GridView列中定义date格式。 例如:

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

这是一个使用从GridView派生的自定义控件的解决scheme:

 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; } } } 

最后我从ScottE得到了答案:

我们必须在datagridview的itemdatabound中添加下面的代码:

protected void dg_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); } } 

}

但是,上面的代码会检查绑定到数据生成的所有数据。它会尝试将数据parsing为单元格中的date时间。如果它是有效的date时间,则会将数据转换为我们所应用的格式。