2014-09-12 9 views
1

我有一個數據閱讀器以這種格式從數據庫讀取字符串2014-07RowDataBound中的字符串格式

在查詢字符串值,我也經常串2014-07我使用字符串格式方法在輸出2014年7月的RowDataBound

//In RowDataBound 
    if (!string.IsNullOrEmpty(Request.QueryString["Month"])) 
    { 
     e.Row.Cells[1].Text = string.Format(CultureInfo.CurrentCulture, "{0:MMMM yyyy}", 
              DataBinder.Eval(e.Row.DataItem, "Month")); 
    } 

我用下面這段代碼爲在我的c#應用程序基於字符串2014-072014年7月

生成新密鑰
Label Month = (Label)row.FindControl("Month"); 

//generate a new key 
DateTime dt; 
DateTime.TryParseExact(Month.Text.ToString(), "MMMM yyyy", 
       CultureInfo.CurrentCulture, DateTimeStyles.None, out dt); 

//Check "-" in string 
if (Month.Text.ToString().IndexOf("-") == -1) 
{ 
    string key = dt.ToString("yyyy-MM"); 
} 
else 
{ 
    string key = Month.Text.ToString(); 
} 

如果字符串2014-07,我不轉換價值2014-072014年7月我有正確的輸出:

2014-07 

如果字符串中轉換的RowDataBound是2014年7月我有不正確的輸出:

0001-01 

這開始讓我相信我的結構作爲一個整體是不正確的。

我缺少什麼?

代碼有什麼問題?

我將不勝感激任何幫助,您可以給我在工作的這個問題。

Edit #1

我曾嘗試打印GridView控件的值時,輸出爲2014年7月和輸出中是空的,爲什麼呢?

Label Month = (Label)row.FindControl("Month"); 

Response.Write(Month.Text.ToString()); 
Response.End(); 

Edit #2

代碼如下完成:

protected void sendValueOfMonth(object sender, EventArgs e) 
{ 
    DateTime d = DateTime.Now; 
    string key; 
    ImageButton ImgSend = (ImageButton)sender; 
    GridViewRow row = (GridViewRow)Lotto_A.NamingContainer; 
    Label Month = (Label)row.FindControl("Month"); 

    DateTime dt; 
    DateTime.TryParseExact(Month.Text.ToString(), "MMMM yyyy", 
        CultureInfo.CurrentCulture, DateTimeStyles.None, out dt); 

    //Check "-" in string 
    if (Month.Text.ToString().IndexOf("-") == -1) 
    { 
     key = dt.ToString("yyyy_MM"); 
    } 
    else 
    { 
     key = Month.Text.ToString(); 
    } 

    Response.Write(Month.Text.ToString() + "<br />"); 
    Response.Write(key.ToString()); 
} 


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     ImageButton ImgSend = (ImageButton)e.Row.FindControl("ImgSend"); 
     Label Month = (Label)row.FindControl("Month"); 


     if (!string.IsNullOrEmpty(Request.QueryString["Month"])) 
     { 
      InitializeCulture(); 
      e.Row.Cells[1].Text = string.Format(CultureInfo.CurrentCulture, "{0:MMMM yyyy}", DataBinder.Eval(e.Row.DataItem, "Month")); 
     } 

    } 
} 


        <asp:TemplateField HeaderText="Send"> 
         <ItemTemplate> 
          <center> 
           <asp:ImageButton ID="ImgSend" runat="server" OnClick="sendValueOfMonth" /> 
          </center> 
         </ItemTemplate> 
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="Month"> 
         <ItemTemplate> 
          <center> 
           <asp:Label ID="Month" runat="server" Text='<%# Eval("Month").ToString() %>'></asp:Label> 
          </center> 
         </ItemTemplate> 
        </asp:TemplateField> 
+0

小心分享前端的來源呢? – jomsk1e 2014-09-12 11:50:44

+0

前端來源? gridView代碼? – 2014-09-12 11:54:34

回答

0

你不需要從你的代碼做任何事情的背後。試試這個:

<asp:Label ID="Month" runat="server" Text='<%# Bind("Month", "{0: yyyy-MM}").ToString() %>'></asp:Label>