KanColle © 2014 DMM.com / KADOKAWA GAMES All Rights Reserved. / Animation by © Good Smile Company
GridView 顯示結果取代成其它文字/格式
#1
GridView 顯示結果取代成其它文字 (例如 1 -> √ 打勾)
1. ASPX
GridView 加入 OnRowDataBound="GridView1_RowDataBound"
程式碼:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderWidth="1px" BackColor="#E7E7FF" CellPadding="3" CellSpacing="2" BorderStyle="None" BorderColor="White" OnRowDataBound="GridView1_RowDataBound" EnableModelValidation="True">
</asp:GridView>
2.ASPX.CS
程式碼:
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        //e.Row.Cells[4] = GridView左邊算來第5列 0,1,2,3,4
        if (e.Row.Cells[4].Text == "0")
        {
        e.Row.Cells[4].Text = "&nbsp;";
        }
        else
        {
        e.Row.Cells[4].Text = "&radic;";
        }
GridView 顯示結果取代成其它格式(例如 20110630 顯示成 2011/06/30)
方法同上,但判斷改為
程式碼:
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
    //如果查詢結果不為空
    if (e.Row.Cells[1].Text != null)
    {
    //定義 myDate 是字串,若有兩個以上的欄位要改,參數名記得改(例 myDate2 myTime)
    string myDate = e.Row.Cells[1].Text;
    //GridView第2欄 = yyyyMMdd -> yyy/MM/dd
    e.Row.Cells[1].Text = DateTime.ParseExact(myDate, "yyyyMMdd", null).ToString("yyyy/MM/dd");
    }
    }
}
若是時間則用 yyyyMMddHHmmss -> yyyy/MM/dd HH:mm:ss
回覆


前往:


正在瀏覽這個主題的使用者: 2 位訪客