Datagridview hierarchical display

Source: Internet
Author: User

The hierarchical display of the datagridview. If you want to find a third-party control, you can enter the datagridview outlook in the codeproject. There is a control that looks good. The following code simply achieves this effect and does not feel necessary to rewrite the control.

You can see the truth in a picture first.

Okay, this function will be available at a Glance. The code below will be provided (the data source binding will be omitted, and the idea will be OK, huh, huh)

Code

# Region draw Cells
Private void dgvalarmlog_rowpostpaint (Object sender, datagridviewrowpostpainteventargs E)
{
If (E. rowindex> = 0)
{
Graphics G = E. graphics;
If (dgvalarmlog. rows [E. rowindex]. cells [0]. value = plusimage | dgvalarmlog. rows [E. rowindex]. cells [0]. value = subimage)
{
Using (Brush gridbrush = new solidbrush (this. dgvalarmlog. gridcolor), backcolorbrush = new solidbrush (color. White), fontbrush = new solidbrush (system. Drawing. color. Black ))
{
// Offline
Rectangle rect = E. rowbounds;
G. fillrectangle (backcolorbrush, rect. x + 30, rect. Y, dgvalarmlog. Width, rect. Height-1 );
// Define the font
Font font = new font ("", 10, fontstyle. Bold );
String STR = dgvalarmlog. Rows [E. rowindex]. cells [3]. value. tostring ();

// Display date
G. drawstring (STR, Font, fontbrush, rect. x + 50, rect. Y + 4 );
}
}
}
}
# Endregion

# Region display sub-Data
Private void dgvalarmlog_cellcontentclick (Object sender, datagridviewcelleventargs E)
{
// Click the image
If (E. columnindex = 0 & E. rowindex> = 0)
{
Object imagevalue = dgvalarmlog. Rows [E. rowindex]. cells [0]. value;
If (imagevalue = plusimage) // expand
{
String [] STRs = (dgvalarmlog. Rows [E. rowindex]. cells [4]. value. tostring (). Split ('~ ');
String sdate = STRs [0]. Trim (); // start time
String edate = STRs [1]. Trim (); // End Time

Dgvalarmlog. Rows [E. rowindex]. cells [0]. value = subimage;

Datarow [] DRS = DT. Select ("happen_date> = '" + sdate + "' and happen_date <= '" + edate + "'");
Int COUNT = 0;
Foreach (datarow DR in DRS)
{
Dgvalarmlog. rows. insert (E. rowindex + 1 + (count ++), new object [] {blankimage, Dr ["describe"]. tostring (), Dr ["serial_no"]. tostring (), Dr ["happen_date"]. tostring (), Dr ["value"]. tostring (), Dr ["pre_value"]. tostring (), Dr ["avg_value"]. tostring ()});
}
}
Else if (imagevalue = subimage) // fold
{
Dgvalarmlog. Rows [E. rowindex]. cells [0]. value = plusimage;
Try
{
Object tempimage = dgvalarmlog. Rows [E. rowindex + 1]. cells [0]. value;
While (tempimage! = Plusimage & tempimage! = Subimage)
{
Dgvalarmlog. Rows. removeat (E. rowindex + 1 );
Tempimage = dgvalarmlog. Rows [E. rowindex + 1]. cells [0]. value;
}
}
Catch {}
}
}
}
# Endregion

Let's look at a second-level directory:

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.