0

I have a GridView with a TemplateField column which shows a DateTime from a DataSource.

<Columns>
    <asp:CommandField ShowEditButton="True" />

    <asp:TemplateField HeaderText="Start Date">
        <EditItemTemplate>
            <asp:TextBox ID="txtDateStart" runat="server" 
                Text='<%# Bind("dtDateStart", "{0:dd/MM/yyyy}") %>'</asp:TextBox>

        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" 
                Text='<%# Bind("dtDateStart", "{0:dd/MM/yyyy}") %>'></asp:Label>
         </ItemTemplate>

    </asp:TemplateField>
</Columns>

Displaying the date in the correct format works as it should. Note that the format starts with DAY followed by MONTH.

When I switch to edit mode, change the date in the TextBox to '31-01-2013' and press the GridView's update-link i get an error: Cannot convert value of parameter 'dtDateStart' from 'System.String' to 'System.DateTime' The error is generated by the GridView not my own code. It happens before the UpdateMethod of my DataSource is called.

When i type '01-31-2012' the data is processed correctly and the value is updated into the database.

Somehow when the date is displayed it uses format dd-MM-yyyy (just as I need it to) But when it reads the new value form the TextBox it uses MM-dd-yyyy

Can somebody please help me?

1
  • This is just a guess, but do you have the current culture of the app set appropriately? It would seem that the default date format for the app or the gridview is still MM-dd-yyyy.
    – Ann L.
    Jan 31, 2013 at 13:28

2 Answers 2

0

thanks for your reply. I do not know about culture settings within the website. I was assuming that proving the Bind-function with a dateformat would be sufficient.

By now I have implemented the following workaround, which is hopefully useful for others:

I have added an additional property to my class which is of type string. This property I only use for binding to the gridview.

// Original Date property //
public DateTime dtDateStart { get; set; }

// Additional Date property of type string // 
public string sDateStart
{
    get
    {
        return dtDateStart.ToString("dd/MM/yyyy");
    }
    set
    {
        dtDateStart = DateTime.ParseExact(value, "dd/MM/yyyy", null);
    }
}

I am not sure if this should be marked as the answer. But maybe others can use this as a quick workaround as well.

-1

I think you can use like this

         <EditItemTemplate>
            <asp:TextBox ID="txtDateStart" runat="server" 
                Text='<%# Convert.ToDateTime(Bind("dtDateStart")).ToString("dd/MM/yyyy") %>'</asp:TextBox>

        </EditItemTemplate>

this weill help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.