Hi Ilanocf,
You need to set the DateTime in yyyy-MM-dd format.
Please refer below sample.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<div class="row">
<div class="col-md-2">
<div>Date:*</div>
<div><asp:TextBox ID="txtDOB" runat="server" CssClass="form-control" placeholder="dd/mm/yyyy" TextMode="Date"></asp:TextBox>
</div>
</div>
</div>
<div class="form-group">
<div class="row" style="margin: 20px 0px 20px 0px;">
<div class="col-sm-12">
<asp:DataGrid ID="dgCustomers" runat="server" AutoGenerateColumns="False" CssClass="table" OnItemCommand="OnItemCommand">
<Columns>
<asp:BoundColumn DataField="FirstName" HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"></asp:BoundColumn>
<asp:BoundColumn DataField="BirthDate" HeaderText="Date Of Birth" DataFormatString="{0:dd/MM/yyyy}"></asp:BoundColumn>
<asp:ButtonColumn CommandName="BirthDate" HeaderText="" Text="Edit"></asp:ButtonColumn>
</Columns>
<HeaderStyle CssClass="bg-success" />
</asp:DataGrid>
</div>
</div>
</div>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 3 FirstName, LastName, BirthDate FROM Employees", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
this.dgCustomers.DataSource = dt;
this.dgCustomers.DataBind();
}
}
}
}
}
}
protected void OnItemCommand(object source, DataGridCommandEventArgs e)
{
string dateofBirth = e.Item.Cells[2].Text;
this.txtDOB.Text = Convert.ToDateTime(dateofBirth).ToString("yyyy-MM-dd");
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 3 FirstName, LastName, BirthDate FROM Employees", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Me.dgCustomers.DataSource = dt
Me.dgCustomers.DataBind()
End Using
End Using
End Using
End Using
End If
End Sub
Protected Sub OnItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs)
Dim dateofBirth As String = e.Item.Cells(2).Text
Me.txtDOB.Text = Convert.ToDateTime(dateofBirth).ToString("yyyy-MM-dd")
End Sub
Screenshot