This is a migrated thread and some comments may be shown as answers.

How to format a DataTime field

1 Answer 66 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nikola
Top achievements
Rank 2
Nikola asked on 14 Nov 2012, 02:22 PM
heeey guys 


I have a question, I have  here a data time field that is saved in the db as date time and it need to be a date time , and I want to load this Field in a drop down but not the hole value just the year 

}
     protected void LoadYear()
     {
         SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HolidayTrackerConnectionString"].ConnectionString);
 
         SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM HtVacationDay", connection);
         DataTable dt = new DataTable();
 
         adapter.Fill(dt);
 
         rcbYear.DataTextField = "FromDate";
         rcbYear.DataValueField = "VacationDayId";
 
         rcbYear.DataSource = dt;
         rcbYear.DataBind();
 
         rcbYear.Items.Insert(0, new RadComboBoxItem("-Select a Year-"));
     }
thanks for help and fast answer ! 

1 Answer, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 15 Nov 2012, 08:02 AM
Hi,

Here is the sample code to show the year alone.
C#:
int date;
protected void LoadYear()
{
  SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HolidayTrackerConnectionString"].ConnectionString);
  SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM HtVacationDay", connection);
  DataTable dt = new DataTable();
  adapter.Fill(dt);
  dt.Columns.Add("MyRow");
  foreach (DataRow dr in dt.Rows)
  {
    date = Convert.ToDateTime(dr["FromDate"]).Year;
    int index = dt.Rows.IndexOf(dr);
    dt.Rows[index]["MyRow"] = date;
  }
   rcbYear.DataSource = dt;
   rcbYear.DataTextField = "MyRow";
   rcbYear.DataValueField = "OrderID";
   rcbYear.DataBind();
   rcbYear.Items.Insert(0, new RadComboBoxItem("-Select a Year-"));
}

Thanks,
Shinu.
Tags
Grid
Asked by
Nikola
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Share this question
or