Hi YulIhnio,
You need to set the formmat to yyyy-MM-ddTHH:mm to assign the value when TextBox TextMode set to DateTimeLocal.
Check the below example.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound" OnRowEditing="OnRowEditing">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name"></asp:BoundField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%# Bind("Date") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDate" runat="server" TextMode="DateTimeLocal" Text='<%#Eval("Date","{0:yyyy-MM-ddTHH:mm}") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton Text="Update" runat="server" OnClick="OnUpdate" />
<asp:LinkButton Text="Cancel" runat="server" OnClick="OnCancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void BindGrid()
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[] {
new System.Data.DataColumn("Id"),
new System.Data.DataColumn("Name"),
new System.Data.DataColumn("Date",typeof(DateTime)) });
dt.Rows.Add(1, "John Hammond", new DateTime(2020, 01, 01));
dt.Rows.Add(2, "Mudassar Khan", new DateTime(2020, 01, 15));
dt.Rows.Add(3, "Suzanne Mathews", new DateTime(2020, 02, 29, 11, 25, 45));
dt.Rows.Add(4, "Robert Schidner", new DateTime(2020, 03, 22));
GridView1.Columns[2].Visible = false;
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["ondblclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Edit$" + e.Row.RowIndex);
e.Row.Attributes["style"] = "cursor:pointer";
}
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
GridView1.Rows[e.NewEditIndex].Attributes.Remove("ondblclick");
GridView1.Columns[2].Visible = true;
}
protected void OnUpdate(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
string name = (row.Cells[0].Controls[0] as TextBox).Text;
DateTime dateTime = Convert.ToDateTime((row.FindControl("txtDate") as TextBox).Text);
GridView1.EditIndex = -1;
this.BindGrid();
}
protected void OnCancel(object sender, EventArgs e)
{
GridView1.EditIndex = -1;
this.BindGrid();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Protected Sub BindGrid()
Dim dt As System.Data.DataTable = New System.Data.DataTable()
dt.Columns.AddRange(New System.Data.DataColumn() {
New System.Data.DataColumn("Id"),
New System.Data.DataColumn("Name"),
New System.Data.DataColumn("Date", GetType(DateTime))})
dt.Rows.Add(1, "John Hammond", New DateTime(2020, 1, 1))
dt.Rows.Add(2, "Mudassar Khan", New DateTime(2020, 1, 15))
dt.Rows.Add(3, "Suzanne Mathews", New DateTime(2020, 2, 29, 11, 25, 45))
dt.Rows.Add(4, "Robert Schidner", New DateTime(2020, 3, 22))
GridView1.Columns(2).Visible = False
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes("ondblclick") = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Edit$" & e.Row.RowIndex)
e.Row.Attributes("style") = "cursor:pointer"
End If
End Sub
Protected Sub OnRowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
Me.BindGrid()
GridView1.Rows(e.NewEditIndex).Attributes.Remove("ondblclick")
GridView1.Columns(2).Visible = True
End Sub
Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, LinkButton)).NamingContainer, GridViewRow)
Dim name As String = (TryCast(row.Cells(0).Controls(0), TextBox)).Text
Dim dateTime As DateTime = Convert.ToDateTime((TryCast(row.FindControl("txtDate"), TextBox)).Text)
GridView1.EditIndex = -1
Me.BindGrid()
End Sub
Protected Sub OnCancel(ByVal sender As Object, ByVal e As EventArgs)
GridView1.EditIndex = -1
Me.BindGrid()
End Sub
Screenshot