Date/Time Databinding

  • Thread starter Thread starter Katty
  • Start date Start date
K

Katty

I insert a single row into a Database, this row has a "Date" column in short
format with value "29/11/2003".
The problem is that when I bind a TextBox to the Date column and then fill
the DataSet, the TextBox
shows "29/11/2003 12:00 a.m" How can I show "just" the date? Any Example?
 
Hi Katty,
Katty said:
I insert a single row into a Database, this row has a "Date" column in short
format with value "29/11/2003".
The problem is that when I bind a TextBox to the Date column and then fill
the DataSet, the TextBox
shows "29/11/2003 12:00 a.m" How can I show "just" the date? Any Example?

Add the following lines after binding data:
Binding b = textBox1.DataBindings["Text"];

b.Format += new ConvertEventHandler(b_Format);

And implement Format event:

private void b_Format(object sender, ConvertEventArgs e)

{

DateTime dt = (DateTime)e.Value;

e.Value = dt.ToString("d");

}

HTH,
 
Katty said:
I insert a single row into a Database, this row has a "Date" column
in short format with value "29/11/2003".
The problem is that when I bind a TextBox to the Date column and then
fill the DataSet, the TextBox
shows "29/11/2003 12:00 a.m" How can I show "just" the date? Any
Example?

Apart from Miha's solution, you can also add formatting in the
databinding expression:

If you use VB:
<asp:TextBox id="x" Text='<%# DataBinder.Eval(Container.DataItem, _
"myDateField","{d}" ) %>' runat="server"> </asp:TextBox>
 
Back
Top