问题描述
大家好我正在使用gridview显示数据,其中我在标题中使用了日期,如01,02,03,04,05到月底,并且工具提示也用于在用户将鼠标移到其上时显示数据描述,但是最后,当我看到它在标题中以yyyy-mm-dd格式显示日期,但我只希望日期没有月份或年份
Hi everyone i am using gridview to display data in which i have used date in header like 01,02,03,04,05 upto end of month and tooltip also used to display data description whenever user move mouse over it, but at end when i see it display date in header in format yyyy-mm-dd but i want only date no month or year
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 1; i <= e.Row.Cells.Count - 1; i++)
{
if (e.Row.Cells[i].Text == "0" || string.IsNullOrEmpty(e.Row.Cells[i].Text) || e.Row.Cells[i].Text == " ")
{
e.Row.Cells[i].Text = "";
}
else
{
e.Row.Cells[i].BackColor = System.Drawing.Color.DarkOrange;
dateSetExport.Tables.Clear();
dateSetExport.Reset();
SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter("@Employe_Id", e.Row.Cells[0].Text.Split('-')[0]);
param[1] = new SqlParameter("@Startdate", gvDetails.HeaderRow.Cells[i].Text);
DataTable dt1 = DataHelper.getDataTableExecuteSP("usp_GetToolTip", param);
dt1.TableName = "ToolTip";
dateSetExport.Tables.Add(dt1);
string tooltip = "";
for (int j = 0; j < dt1.Rows.Count; j++)
{
tooltip = tooltip + dt1.Rows[j]["normal_working_hours"].ToString() + " Hours : " + dt1.Rows[j]["description"].ToString() + "\n\n";
}
e.Row.Cells[i].ToolTip = tooltip;
}
//date = date.Split('-')[0] + "-" + date.Split('-')[1] + "-" + Convert.ToString(Convert.ToInt32(date.Split('-')[2]) + 1);
//if(date=="2017-07-31")
//{
// date = date.Split('-')[0] + "-" + Convert.ToString(Convert.ToInt32(date.Split('-')[1])+1)+ "-" + Convert.ToString(Convert.ToInt32(date.Split('-')[2]) -30);
//}
}
}
e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
if (e.Row.RowIndex == 1)
{
//Creating a gridview object
GridView objGridView = (GridView)sender;
//Creating a gridview row object
GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
//Creating a table cell object
TableHeaderCell objtablecell = new TableHeaderCell();
foreach (TableCell tblCell in e.Row.Cells)
{
objtablecell = new TableHeaderCell();
objgridviewrow.Cells.Add(objtablecell);
}
objgridviewrow.ID = "NewHeader";
objgridviewrow.BackColor = System.Drawing.Color.FromArgb(0xf0, 0x9b, 0x16);
objgridviewrow.ForeColor = System.Drawing.Color.Black;
objGridView.Controls[0].Controls.AddAt(0, objgridviewrow);
GridViewRow firstRow = (GridViewRow)gvDetails.FindControl("NewHeader");
GridViewRow secondRow = gvDetails.HeaderRow;
for (int i = secondRow.Cells.Count - 2; i >= 0; i--)
{
if (secondRow.Cells[i].Text.Substring(5,2) == secondRow.Cells[i + 1].Text.Substring(5,2))
{
firstRow.Cells[i].ColumnSpan = firstRow.Cells[i+1].ColumnSpan < 2 ? 2 : firstRow.Cells[i+1].ColumnSpan + 1;
firstRow.Cells[i + 1].Visible = false;
firstRow.Cells[i].Text = CellText(secondRow.Cells[i].Text.Substring(5, 2)) + " " + secondRow.Cells[i].Text.Substring(0, 4);
firstRow.Cells[i].HorizontalAlign = HorizontalAlign.Center;
}
if (i== 1 && (firstRow.Cells[1].Text==""))
{
firstRow.Cells[1].Text = CellText(secondRow.Cells[1].Text.Substring(5, 2)) + " " + secondRow.Cells[1].Text.Substring(0, 4);
firstRow.Cells[1].HorizontalAlign = HorizontalAlign.Center;
}
else if (i == 0 && (firstRow.Cells[firstRow.Cells.Count - 1].Text == ""))
{
string fullText = CellText(secondRow.Cells[secondRow.Cells.Count - 1].Text.Substring(5, 2)) + " " + secondRow.Cells[secondRow.Cells.Count - 1].Text.Substring(0, 4);
firstRow.Cells[firstRow.Cells.Count - 1].Text = fullText;
firstRow.Cells[firstRow.Cells.Count - 1].HorizontalAlign = HorizontalAlign.Center;
}
}
//foreach (TableCell tblCell in gvDetails.HeaderRow.Cells)
//{
// if (tblCell.Text != "Employee Name")
// {
// tblCell.Text = tblCell.Text.Substring(8, 2);
// }
//}
}
}
这是我的网格
This is my grid
<asp:GridView ID="gvDetails" runat="server" Style="font-size: 13px; width: 100%;" CssClass="grid clsWrap" AutoGenerateColumns="True" OnRowDataBound="gvDetails_RowDataBound">
<HeaderStyle Font-Bold="False" CssClass="Font gvheaderstyle" Wrap="False"/>
<PagerStyle CssClass="gridB" ForeColor="WhiteSmoke" />
<RowStyle HorizontalAlign="Center" />
</asp:GridView>
我的尝试:
What I have tried:
//foreach (TableCell tblCell in gvDetails.HeaderRow.Cells)
//{
// if (tblCell.Text != "Employee Name")
// {
// tblCell.Text = tblCell.Text.Substring(8, 2);
// }
//}
每个转换日期的格式为i想但是如果我取消注释这个foreach它会影响每一行的工具提示,如果我在gridview的标题中注释它的效果日期会导致读取一行的数据yyyy-mm-dd但我只想要一天
帮帮我
this for each converts date in the format i want but if i uncomment this foreach it effect tooltip in each row that result in reading data of one row if i comment it effect date in header of gridview results in yyyy-mm-dd but i want only day
help me
推荐答案
<asp:GridView ID="gvDetails" runat="server" Style="font-size: 13px; width: 100%;" CssClass="grid clsWrap" AutoGenerateColumns="True" OnRowDataBound="gvDetails_RowDataBound">
<HeaderStyle Font-Bold="False" CssClass="Font gvheaderstyle" Wrap="False"/>
<PagerStyle CssClass="gridB" ForeColor="WhiteSmoke" />
<RowStyle HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="Date" DataFormatString="{0:MM/dd/yyyy}" HeaderText="Order Date" >
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name" >
</asp:BoundField>
</asp:GridView>
请注意DataFied =Date的第一个Bound字段具有DataFormat String的属性。你可以改变你想要的结果。
Note the first Bound field with DataFied="Date" has property of DataFormat String. you can change for your desired result.
这篇关于如何在gridview的标题中更改日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!