C# 当我从gridview更新日期时,日期和月份将始终切换位置。我希望它是dd/mm/yyyy格式

C# 当我从gridview更新日期时,日期和月份将始终切换位置。我希望它是dd/mm/yyyy格式,c#,winforms,date,gridview,C#,Winforms,Date,Gridview,我编辑并更改了第一行的日期条目,从2014年4月5日更改为2014年4月6日。单击更新后,它将变为2014年6月4日。“我的日期输入”列是日期类型格式,而不是用于提供信息的日期时间格式。我希望它是dd/mm/yyyy格式 DateTime datDate; if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" }, System.Globalization.CultureInf

我编辑并更改了第一行的日期条目,从2014年4月5日更改为2014年4月6日。单击更新后,它将变为2014年6月4日。“我的日期输入”列是日期类型格式,而不是用于提供信息的日期时间格式。我希望它是dd/mm/yyyy格式

DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" },
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None, out datDate))

{
   // Call your update method with 
}
else
{
 //Show validation error message
}

DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" },
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None, out datDate))

{
   // Call your update method with 
}
else
{
 //Show validation error message
}

DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" },
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None, out datDate))

{
   // Call your update method with 
}
else
{
 //Show validation error message
}

DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" },
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None, out datDate))

{
   // Call your update method with 
}
else
{
 //Show validation error message
}

DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" },
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None, out datDate))

{
   // Call your update method with 
}
else
{
 //Show validation error message
}

如果我在日期中输入的值高于12。等16。将出现错误。

DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" },
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None, out datDate))

{
   // Call your update method with 
}
else
{
 //Show validation error message
}

DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" },
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None, out datDate))

{
   // Call your update method with 
}
else
{
 //Show validation error message
}

DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" },
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None, out datDate))

{
   // Call your update method with 
}
else
{
 //Show validation error message
}
遵循@Arshad建议时出错

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack == false)
    {
        bindResultGridView();

    }
    //Logout.Visible = false;
    string memName = (String)Session["UserName"];
    lblUsername.Text = String.Concat("Welcome Guest!");

    if (Session["Username"] != null && Session["Username"] != String.Empty)
    {
        lblUsername.Text = "Welcome, " + memName + "!";


    }

}


private void bindResultGridView()
{
    String ConStr = ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(ConStr);

    try
    {

        String SQL = null;

        SQL = "SELECT BlogID, Name, Blogtype, Description, Dateentry FROM [EntryTable]";

        SqlCommand cmd = new SqlCommand(SQL, con);
        con.Open();

        SqlDataReader reader = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(reader);
        grdBlog.DataSource = dt;
        grdBlog.DataBind();


        reader.Close();
    }

    finally
    {
        con.Close();
    }
}
protected void grdBlog_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    grdBlog.EditIndex = -1;
    bindResultGridView();
}

protected void grdBlog_RowEditing(object sender, GridViewEditEventArgs e)
{
    grdBlog.EditIndex = e.NewEditIndex;
    bindResultGridView();
}
protected void grdBlog_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int selectedRow = e.RowIndex;   //get selected row
    //  get product id from data key
    int blogid = (int)grdBlog.DataKeys[selectedRow].Value;

    //  get current grid view row
    GridViewRow row = (GridViewRow)grdBlog.Rows[selectedRow];
    TextBox name = (TextBox)row.FindControl("txtName");
    //  find text box for txtPrice
    TextBox blogtype = (TextBox)row.FindControl("txtBlogType");
    TextBox description = (TextBox)row.FindControl("txtDescription");
    TextBox dateentry = (TextBox)row.FindControl("txtDateEntry");
    //  Remove $ sign
    string strName = name.Text;
    string strBlogType = blogtype.Text;
    string strDescription = description.Text;
    string strDateEntry = dateentry.Text;
    DateTime datDate;
    if (DateTime.TryParseExact(strDateEntry, new string[] { "dd/MM/yyyy" },
                           System.Globalization.CultureInfo.InvariantCulture,
                           System.Globalization.DateTimeStyles.None, out datDate))
    {
        updateBlogGridviewRecord(blogid, strName, strBlogType, strDescription, datDate.ToString());
    }

    else
    {

    }
}

private void updateBlogGridviewRecord(int blogid, string strName, string strBlogType, string strDescription, string strDateEntry)
{

    string strConnectionString = ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString;
    SqlConnection myConnect = new SqlConnection(strConnectionString);

    string strCommandText = "UPDATE EntryTable SET [Name]=@Name, [BlogType]=@BlogType, [Description]=@Description, [DateEntry]=@DateEntry WHERE [BlogID]=@BlogID";

    SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
    cmd.Parameters.AddWithValue("@BlogID", blogid);
    cmd.Parameters.AddWithValue("@Name", strName);
    cmd.Parameters.AddWithValue("@BlogType", strBlogType);
    cmd.Parameters.AddWithValue("@DateEntry", strDateEntry);
    cmd.Parameters.AddWithValue("@Description", strDescription);
    myConnect.Open();

    int result = cmd.ExecuteNonQuery();

    if (result > 0)
    {
        lblError.Text = "Record updated!";
    }
    else
    {
        lblError.Text = "Update fail";
    }

    myConnect.Close();


    //Cancel Edit Mode
    grdBlog.EditIndex = -1;
    bindResultGridView();
}
DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" },
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None, out datDate))

{
   // Call your update method with 
}
else
{
 //Show validation error message
}

DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" },
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None, out datDate))

{
   // Call your update method with 
}
else
{
 //Show validation error message
}
源代码

    <asp:GridView ID="grdBlog" runat="server" style=
"margin-left: 0px" Width="695px" Height="147px" AutoGenerateColumns="False" 
                        onrowcancelingedit="grdBlog_RowCancelingEdit" onrowediting="grdBlog_RowEditing" 
                        onrowupdating="grdBlog_RowUpdating" DataKeyNames="BlogID" 
                        onrowdeleting="grdBlog_RowDeleting" AllowPaging="True" 
                        onpageindexchanging="grdBlog_PageIndexChanging" PageSize="5">
                        <Columns>
                            <asp:BoundField DataField="BlogID" HeaderText="BlogID" 
                                SortExpression="BlogID" />
                            <asp:TemplateField HeaderText="Name">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="BlogType">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtBlogType" runat="server" Text='<%# Bind("BlogType") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("BlogType") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Description">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtDescription" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="DateEntry">
   <EditItemTemplate>
    <asp:TextBox ID="txtDateEntry" runat="server" Text='<%# Eval("DateEntry", "{0:dd/MM/yyyy}") %>'>
 </asp:TextBox>
 </EditItemTemplate>
<ItemTemplate>
  <asp:Label ID="Label4" runat="server" Text='<%# Eval("DateEntry", "{0:dd/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
                            </asp:TemplateField>
                            <asp:CommandField ShowEditButton="True" />
                            <asp:TemplateField ShowHeader="False">
                                <ItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                                        CommandName="Delete" Text="Delete" OnClientClick="javascript : return confirm('Confirm delete this record?');"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" },
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None, out datDate))

{
   // Call your update method with 
}
else
{
 //Show validation error message
}

使用
'
而不是
'

DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" },
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None, out datDate))

{
   // Call your update method with 
}
else
{
 //Show validation error message
}
你可以这样试试

DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" },
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None, out datDate))

{
   // Call your update method with 
}
else
{
 //Show validation error message
}
<asp:TemplateField>
  <ItemTemplate>
     <asp:Label ID="lblDate" runat="server" Text='<%# Eval("Date", "{0:dd/MM/yyyy}")%>' >
     </asp:Label>
  </ItemTemplate>
</asp:TemplateField>

您可以在客户端像这样验证日期时间

DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] { "dd/MM/yyyy" },
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None, out datDate))

{
   // Call your update method with 
}
else
{
 //Show validation error message
}
<asp:TemplateField HeaderText="date" SortExpression="date">
    <EditItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("date") %>'></asp:TextBox>
                <asp:RangeValidator ID="RangeValidator1" runat="server" BackColor="Red" ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="Invalid Date" MaximumValue="28/12/9999" MinimumValue="28/12/1000" SetFocusOnError="True" Type="Date"></asp:RangeValidator>
        </EditItemTemplate>
        <ItemTemplate>
                <asp:Label ID="lblDate" runat="server" Text='<%# Eval("date", "{0:dd/MM/yyyy}")%>'>
                </asp:Label>
        </ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />


Erm将其放置在何处?我在上面粘贴了我的源代码。我还添加了一个图像,没有格式@arshaddorry,我最后的回答是针对windows应用程序的。请检查更新的annserI更新我的源代码上面。更新时,它仍然会在dd和mm之间更改位置@Arshadcan您可以更改
SQL=“从[EntryTable]中选择BlogID、名称、Blogtype、描述、日期项”上面更新的查询。还是一样@ArshadI更新了上面的源代码。更新时,它仍然会在dd和mm之间更改位置@Chirag Adhvaryu,如果我在日期中输入的值高于12。等16。会有错误。我添加了图像@ChiragAdhvaryu@Jordjmax为此,您可以使用JavaScript或regexErm wat验证日期;我不知道怎么做@希拉格Adhvaryu@Jordjmax我发布了关于使用范围验证器进行日期验证的问题的答案,这个简单的程序将帮助您理解