Display time in TreeView format

Source: Internet
Author: User

:

/// <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 ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.