:
/// <Summary>
/// Binding time
/// </Summary>
/// <Param name = "dtSource"> data source </param>
/// <Param name = "columnName"> used to display the column name of the Time column </param>
/// <Param name = "sort"> sorting method: desc, asc </param>
Private void BindTreeViewDate (DataTable dtSource, string columnName, string sort)
{
DataTable dt = new DataTable ();
DataColumn [] cols = new DataColumn [1];
Cols [0] = new DataColumn ();
Cols [0]. ColumnName = "sortDate ";
Dt. Columns. Add (cols [0]);
Dt. PrimaryKey = cols;
Foreach (DataRow row in dtSource. Select ("1 = 1", columnName + "" + sort ))
{
Object obj = row [columnName];
If (dt. Rows. Find (obj) = null)
{
Dt. Rows. Add (obj );
}
}
TvDate. Nodes. Clear ();
TreeNode nodeSelect = new TreeNode ();
TreeNode nodeSelectM = new TreeNode ();
TreeNode nodeSelectY = new TreeNode ();
If (dt! = Null & dt. Rows. Count> 0)
{
// Bind the first line of time
TreeNode tn1 = new TreeNode ();
TreeNode tn2 = new TreeNode ();
TreeNode tn3 = new TreeNode ();
DateTime date = Convert. ToDateTime (dt. Rows [0] [0]. ToString ());
Tn1.Text = date. Year. ToString () + "Year ";
Tn1.ToolTip = date. tow.datestring ();
TvDate. Nodes. Add (tn1 );
Tn2.Text = date. Month. ToString () + "Month ";
Tn2.ToolTip = date. tow.datestring ();
Tn1.ChildNodes. Add (tn2 );
Tn3.Text = date. Day. ToString () + "Day ";
Tn3.ToolTip = date. tow.datestring ();
Tn2.ChildNodes. Add (tn3 );
Dt. Rows. RemoveAt (0); // remove the first row
Foreach (DataRow row in dt. Rows)
{
DateTime dateNext = Convert. ToDateTime (row [0]. ToString ());
// Year of binding
If (string. Compare (dateNext. Year. ToString () + "Year", tn1.Text )! = 0)
{
Tn1 = new TreeNode ();
Tn2 = new TreeNode ();
Tn3 = new TreeNode ();
Tn1.Text = dateNext. Year. ToString () + "Year ";
Tn1.ToolTip = dateNext. tow.datestring ();
TvDate. Nodes. Add (tn1 );
Tn2.Text = dateNext. Month. ToString () + "Month ";
Tn2.ToolTip = dateNext. tow.datestring ();
Tn1.ChildNodes. Add (tn2 );
Tn3.Text = dateNext. Day. ToString () + "Day ";
Tn3.ToolTip = dateNext. tow.datestring ();
Tn2.ChildNodes. Add (tn3 );
If (nodeSelectY = null & dateNext. Year. ToString () = DateTime. Now. Year. ToString ())
{
NodeSelectY = tn1;
}
}
// Bind to the month
Else if (string. Compare (dateNext. Month. ToString () + "Month", tn2.Text )! = 0)
{
Tn2 = new TreeNode ();
Tn3 = new TreeNode ();
Tn2.Text = dateNext. Month. ToString () + "Month ";
Tn2.ToolTip = dateNext. tow.datestring ();
Tn1.ChildNodes. Add (tn2 );
Tn3.Text = dateNext. Day. ToString () + "Day ";
Tn3.ToolTip = dateNext. tow.datestring ();
Tn2.ChildNodes. Add (tn3 );
If (nodeSelectM = null & dateNext. ToString ("yyyyMM") = DateTime. Now. ToString ("yyyyMM "))
{
NodeSelectM = tn2;
}
}
// Binding day
Else
{
Tn3 = new TreeNode ();
Tn3.Text = dateNext. Day. ToString () + "Day ";
Tn3.ToolTip = dateNext. tow.datestring ();
Tn2.ChildNodes. Add (tn3 );
If (dateNext. tow.datestring () = DateTime. Now. tow.datestring ())
{
NodeSelect = tn3;
}
}
}
}
TvDate. CollapseAll ();
}
The above code is in ASP. NET. If it is used in WinForm, change "ToolTip" to "Tag" and change "ChildNodes" to "Nodes ".