C# 行数据绑定中的字符串格式

C# 行数据绑定中的字符串格式,c#,C#,我有一个数据读取器,可以从数据库中以这种格式读取字符串2014-07 在查询字符串值中,我始终使用字符串2014-07,并使用字符串格式方法在行数据绑定中的输出2014年7月: //In RowDataBound if (!string.IsNullOrEmpty(Request.QueryString["Month"])) { e.Row.Cells[1].Text = string.Format(CultureInfo.CurrentCulture,

我有一个数据读取器,可以从数据库中以这种格式读取字符串2014-07

在查询字符串值中,我始终使用字符串2014-07,并使用字符串格式方法在行数据绑定中的输出2014年7月

    //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"));
    }
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();
}
我使用下面的代码在我的c#应用程序中基于字符串2014-072014年7月生成一个新密钥:

    //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"));
    }
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-07转换为2014年7月,则我有正确的输出:

2014-07
0001-01
如果在行数据绑定中转换的字符串为2014年7月,则我的输出不正确:

2014-07
0001-01
这让我开始相信我的整体结构是不正确的

我错过了什么

代码怎么了

我将非常感谢你在解决这个问题上给我的任何帮助

编辑#1

当输出为2014年7月且输出为空时,我尝试打印gridview的值,为什么

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

Response.Write(Month.Text.ToString());
Response.End();
编辑#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>
受保护的void sendValueOfMonth(对象发送方,事件参数e)
{
DateTime d=DateTime.Now;
字符串键;
ImageButton ImgSend=(ImageButton)发送器;
GridViewRow行=(GridViewRow)Lotto_A.NamingContainer;
Label Month=(Label)row.FindControl(“月”);
日期时间dt;
DateTime.TryParseExact(Month.Text.ToString(),“MMMM yyyy”,
CultureInfo.CurrentCulture,DateTimeStyles.None,out dt);
//在字符串中勾选“-”
if(Month.Text.ToString().IndexOf(“-”)==-1)
{
key=dt.ToString(“yyyy_-MM”);
}
其他的
{
key=Month.Text.ToString();
}
Response.Write(Month.Text.ToString()+“
”); Response.Write(key.ToString()); } 受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e) { 如果(e.Row.RowType==DataControlRowType.DataRow) { ImageButton ImgSend=(ImageButton)e.Row.FindControl(“ImgSend”); Label Month=(Label)row.FindControl(“月”); 如果(!string.IsNullOrEmpty(Request.QueryString[“Month”])) { 初始化文化(); e、 Row.Cells[1]。Text=string.Format(CultureInfo.CurrentCulture,“{0:MMMM yyyy}”,datainder.Eval(e.Row.DataItem,“Month”); } } }
您不需要从代码背后执行任何其他操作。试试这个:

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


是否也要共享前端的源代码?前端源代码?gridView代码?