. Net winform datagridview implement multi-row Header

Source: Internet
Author: User

Recently, we added the Smart Client feature to our B/S system. Indeed, smart clients are very useful. Unfortunately, winform controls are not very powerful, which is much worse than VB. For example, the datagridview datagridoes not support two-dimensional headers, but does not support the total at the end of the table. In contrast, the Web is much easier to handle (like web layout and navigation, but like the operability of win, hope that WPF will become popular soon ).

However, the MIS system does not have a two-dimensional Header which is unacceptable to the customer. It tried the COM control FlexGrid or the Open Source sourcegrid3, but it was not very useful. So I wanted to modify the dview. My approach is to work with cellpainting. It took a day to try, just to make a prototype, not yet complete, and hope that there will be fewer detours for friends in need.

1. inherit the datagridview and add the header information class.
2. Add cellpainting. The Code is as follows: Private void datagridviewex_cellpainting (Object sender, datagridviewcellpaintingeventargs E)
{
If (E. rowindex =-1)
{
// Int W = maid + maid. topleftheadercell. Size. Width + maid [0]. Width + 10;


Rectangle newrect = new rectangle (E. cellbounds. x + 1,
E. cellbounds. Y + 1, E. cellbounds. Width-4,
E. cellbounds. Height-4 );

Using (
Brush gridbrush = new solidbrush (this. gridcolor ),
Backcolorbrush = new solidbrush (E. cellstyle. backcolor ))
{
Using (PEN gridlinepen = new pen (gridbrush ))
{
// Erase the cell.
E. Graphics. fillrectangle (backcolorbrush, E. cellbounds );

// Draw the grid lines (only the right and bottom lines;
// Datagridview takes care of the others ).
E. Graphics. drawline (gridlinepen, E. cellbounds. Left,
E. cellbounds. Bottom-1, E. cellbounds. Right-1,
E. cellbounds. Bottom-1 );
If (E. columnindex>-1 & toprow! = NULL & toprow. cells [E. columnindex]. colspan> 1)
{
E. Graphics. drawline (gridlinepen, E. cellbounds. Right-1,
E. cellbounds. Top + E. clipbounds. Height/2, E. cellbounds. Right-1,
E. cellbounds. Bottom );
}
Else
{
E. Graphics. drawline (gridlinepen, E. cellbounds. Right-1,
E. cellbounds. Top, E. cellbounds. Right-1,
E. cellbounds. Bottom );
}

// Draw the inset highlight box.
// E. Graphics. drawrectangle (pens. Blue, newrect );

Int scale = E. cellbounds. Height/3;
If (E. columnindex>-1 & toprow. cells [E. columnindex]. Text! = NULL)
{
Scale = E. cellbounds. Height/2;
E. graphics. drawline (gridlinepen, E. cellbounds. left, E. cellbounds. bottom-e. cellbounds. height/2, E. cellbounds. right, E. cellbounds. bottom-e. cellbounds. height/2 );
}
// Draw the text content of the cell, ignoring alignment.



If (E. value! = NULL)
{
E. Graphics. drawstring (E. value. tostring (), E. cellstyle. Font,
Brushes. Crimson, E. cellbounds. x + 2,
E. cellbounds. Y + scale + 2, stringformat. genericdefault );



}




If (E. columnindex>-1 & toprow. cells [E. columnindex]. relateindex>-1 & toprow. cells [E. columnindex]. Text! = NULL)

{
Rectangle reccell = new rectangle (E. cellbounds. X-1-toprow. cells [E. columnindex]. spanrowwith,
E. cellbounds. Y + 1, toprow. cells [E. columnindex]. spanrowwith,
E. cellbounds. Height/2 );


Stringformat Sf = new stringformat ();

SF. Alignment = stringalignment. Center;


E. Graphics. drawstring (toprow. cells [E. columnindex]. Text, E. cellstyle. Font, brushes. Crimson, reccell, SF );

}

E. Handled = true;
}
}
}

}

3. Call the method maid [2]. Text = "warehouse receiving ";
Datagridviewex1.toprow. cells [2]. colspan = 2;


Datagridviewex1.toprow. cells [4]. Text = "outbound ";
Datagridviewex1.toprow. cells [4]. colspan = 2;

4,

As for the total end of the table, a prototype is also made. The two-dimensional header + Table end total basically meets the requirements.

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.