MyDataSource is a datasource stored in a session passed through a search page
protected void Page_Load(object sender, EventArgs e)
{
gridview1.DataSource = Session["MyDataSource"];
gridview1.DataBind();
}
gridview1 is a gridview with no datasource waiting for the page load event to bind it to a datasource, in my case it is MyData开发者_运维知识库Source
<asp:GridView ID="gridview1" runat="server" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black"
GridLines="None">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
</asp:GridView>
dates appearing in this gridview display as M/d/yyyy + time for example 12/31/2010 00:00:00
My Question: i need a way to display date as d/M/yyyy with no time for example 31/12/2010 usually i do this by setting the gridview properties htmlencode=false and dateformatstring="{0:M-dd-yyyy}" but in this case the gridview dont show any field because it bind data only at run time
please i need your help in this issue ASAP. thanks in advance
Try to use this code:
<asp:GridView ID="gridview1" runat="server" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black"
GridLines="None" AutoGenerateColumns="false">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
<Columns>
<asp:BoundField DataField="PropertyName" />
<asp:BoundField DataField="PropertyName" />
<asp:BoundField DataField="DateTimeProperty" DataFormatString="{0:dd/M/yyyy}" />
</Columns>
It works fine in my computer. Hope it will help you.
Best regards, Dima.
The gridview won't show any data unless you tell it what to show.
A quick fix would be something like this:
<asp:GridView ID="gridview1" runat="server" OnRowDataBound="gridview1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="dateLabel" runat="server" />
<ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and then in your code-behind:
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null)
{
var item = (MyCustomDataBoundObject)e.Row.DataItem;
var dateLabel = (Label)gridview1.FindControl("dateLabel");
dateLabel.Text = item.Date.ToString("dd/MM/yyyy");
}
}
The above is untested, but should be enough to get you what you're asking.
Out of curiosity, why are you passing the datasource through the session? I don't know anything about your app, but it's never an approach I've taken before. Why not just bind the gridview to an objectdatasource which you've defined in your aspx page? Then you could use a simple bound column.
精彩评论