无法在数据集列GridView格式化日期 [英] Unable to format date in dataset column,GridView
问题描述
由于我绑定数据集中的数据,我没有任何模板列或绑定列集,所以我不知道在哪里设置HtmlEncode =FalseDataFormatString ={0:T}
有没有办法这样做。请帮助我。
请找到以下代码示例。
string OleDbConnection =Provider = Microsoft.Jet.OLEDB.4.0; Data Source =+ FileUpload1.PostedFile.FileName + ;扩展属性= \Excel 8.0; HDR =是; 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 +$]; //我想找到这个工作表名称
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 [rowcount] [date_column]。ToString( )] ToString(d);
但是该值没有被分配为mm / dd / yyyy它也是再次使用默认时间mm / dd / yyyy hh:mm:ss AM)。
====================
$ b $我只是将数据集分配给gridview。问题是数据集读取日期列格式为mm / dd / yyyy hh:mm:ss AM.I无法更改数据集中的数据还有。
=============================== ==========================
决赛我得到了ScottE的答案:
我们必须在datagridview的itemdatabound中添加以下代码:
protected void dgValidatedData_ItemDa taBound1(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是需要格式化的日期的列名称(可以是多个)。这当然是vb.net,但是你可以排除这一点。我确定有一个更好的方法,但这是有用的。
受保护的子gv_RowDataBound(ByVal sender As Object,ByVal e As System .Web.UI.WebControls.GridViewRowEventArgs)
如果e.Row.RowType = DataControlRowType.DataRow Then
For i As Integer = 0 To e.Row.Cells.Count - 1
如果gv .HeaderRow.Cells(i).Text =Item然后
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)
如果e.Row.RowType = DataControlRowType.DataRow然后
对于i As Integer = 0到e.Row.Cells.Count - 1
Dim cellDate As Date
如果Date.TryParse(e.Row。单元格(i).Text,cellDate)然后
e.Row.Cells(i).Text = String.Format({0:d},cellDate)
End If
Next
End If
End Sub
I am reading data from an excel sheet and displaying it in a data gridview.There are some date columns in the excel.So when i read the data from the excel and bind it to the dataGridView.The date is displayed in the format "02/02/2009 12:00:00 AM" but the actual data in the excel column is in the format "2/2/2009".So how to change the date format in the datagridview.
Since i am binding the data from the dataset i dont have any template columns or bound column set so i dont know where to set the HtmlEncode="False" DataFormatString = "{0:T}"
Is there any way to do this.Please help me.
Please find the below code sample.
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();
========================================================== I tried the
dsExcellData.Tables[0].Rows[rowcount]["date_column"].ToString()] = dsExcellData.Tables[0].Rows[rowcount]["date_column"].ToString()].ToString("d");
but the value is not getting assigned as "mm/dd/yyyy" It is also taking the time default time again (mm/dd/yyyy hh:mm:ss AM).
=============================================================
I am just assigning the data set to the gridview.The problem is the dataset is reading the date column in the format mm/dd/yyyy hh:mm:ss AM.I am unable to change the data in the dataset also.
=============================================================
Finaly i got the answer from ScottE:
we have to add the below code in the itemdatabound of the datagridview :
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);
}
}
}
Ok, try this, where "Item" is the column name (could be multiple) that is a date that needs formatting. This is of course vb.net, but you can sort that out. I'm sure there's a better way, but this works.
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
Or, if you don't know what columns will have dates, the following will work as well:
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
这篇关于无法在数据集列GridView格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!