Msdn teaches you how to beautify the control of the datagridview

Source: Internet
Author: User

When I started winform development, I thought everyone had the same question as me.

Why is the development control provided by windows so beautiful ". It can even be described as normal.

After a long time, we found that we could re-paint the widget's appearance through the onpaint event. Soon after, the second question came. Onpaint events are useful for single controls such as buttons and panel, but what should we do for composite controls such as the datagridview, toolbar, and tabcontrol?

This problem has indeed plagued "Xiao Bai" for a long time, and even doubted Microsoft's "capabilities" (mostly Xiao Bai ).

Fortunately, I had a good habit of turning over "tianshu" when I was okay, because it could always surprise me.

Now, let me teach you how to beautify the dview (actually, msdn teaches us ).

Note: As I am too old to catch up with new technologies, Vs and msdn have been using vs2005.

All the explanations are from msdn2005. If there is a gap with your environment, please forgive me.

First, open msdn and enter the datagridview in the condition box on the index page. At this time, you will see a lot of introductions related to the datagridview. What we need to pay attention to is:Custom.

Msdn address: MS-help: // Ms. msdnqtr. v80.chs/MS. msdn. v80/MS. visual Studio. v80.chs/dv_fxmclictl/html/01ea5d4c-a736-4596-b0e9-a67a1b86e15f.htm ")

 

As shown in the figureFluorescence LabelingIs what we need to pay attention to and use. In fact, Microsoft has always been ready for the solution, just waiting for those who need it to use it.

 

The areas to be repainted by the datagridview are roughly divided into three parts:Column, rowheader, selectrow.

We will first drawColumn and rowheader

Cellpainting event Is the event we want to use.

Msdn description:

Note: This event is added in. NET Framework 2.0..

Occurs when the cell needs to be drawn..

Namespace: system. Windows. Forms
ProgramSet: system. Windows. Forms (in system. Windows. Forms. dll)

 

There are few descriptions, but the example is enough.

Before using the event, let's take a look at the event parameters. After learning about it, everything is easier for us.

Cellpainting (Object sender, system. Windows. Forms. datagridviewcellpaintingeventargs E)

ObjectFor the time being, no matter (you should know it). Let's take a look at the datagridviewcellpaintingeventargs.

 

DatagridviewcellpaintingeventargsMany useful methods and attributes are provided. Here I will only introduce the attributes and methods required for repainting.

Cellbounds

Obtain the boundary of the current datagridviewcell (Note: We want to plot the area size (column or row)).

Columnindex

Obtain the column index of the current datagridviewcell (Note: column index value).

Rowindex

Obtain the row index of the current datagridviewcell (Note: index value of row).

Graphics

Obtain graphics (Note: Our canvas).

Handled

Gets or sets a value that indicates whether the event handler has fully handled the event, or whether the system should continue to process the event. (Note: Do you need to continue plotting?)

Paintcontent
Draws the content of cells in the corresponding area of the specified boundary. (Note: I just want to say that the words above column and row cannot be forgotten.)

 

Now that we are familiar with these attributes, we will start to draw them.

First, inherit the system's datagridview, reloadCellpainting event

 

 
/// <Summary>
/// Custom dview Control
 
/// </Summary>
 
Public ClassTestpolicirdview: system. Windows. Forms. datagridview
 
{
 
/// <Summary>
 
/// Redraw column and row
 
/// </Summary>
 
/// <Param name = "E"> </param>
 
Protected Override VoidOncellpainting (maid E)
 
{
// For Column
 
If(E. rowindex =-1 ){
 
Drawcolumnandrow (E );
 
E. Handled =True;
 
// For rowheader
 
}Else If(E. columnindex <0 & E. rowindex> = 0 ){
 
Drawcolumnandrow (E );
 
E. Handled =True;
 
}
 
}
 

}

 

PassE. rowindex =-1You can determine whether the current redraw area isColumn.

PassE. columnindex <0 & E. rowindex> = 0You can determine whether the current redraw area isRowheader.

E. Handled = trueYes. Tell the system that you have already re-painted it. This is the end of your task.

The preparation is completed before the painting.

 
 /// <Summary>
 
/// Draw column and rowheader
 
/// </Summary>
 
/// <Param name = "E"> </param>
 
VoidDrawcolumnandrow (maid E)
{
 
// Draw the background color
 
Using(Lineargradientbrush backbrush =
 
NewLineargradientbrush (E. cellbounds,
 
Professionalcolors. menuitempressedgradientbegin,
 
Professionalcolors. menuitempressedgradientmiddle
 
, Lineargradientmode. Vertical )){
 
 
 
Rectangle border = E. cellbounds;
 
Border. Width-= 1;
 
// Fill the painting Effect
 
E. Graphics. fillrectangle (backbrush, border );
// Draw the text information of column and row
 
E. paintcontent (E. cellbounds );
 
// Draw a border
 
Controlpaint. drawborder3d (E. Graphics, E. cellbounds, border3dstyle. etched );
 
 
 
}
}

  

Lineargradientbrush backbrushCreate a gradient Paint Brush

E. Graphics. fillrectangle (backbrush, Border)Paint Brush effect in current region

E. paintcontent (E. cellbounds)Do not forget the text above column or row

Controlpaint. drawborder3dAdding a border is purely a personal hobby. Haha

 

The most exciting moment is coming. Let's take a look at the effect.

 

Before re-painting:

 

After re-painting:

  

Column and rowheaderThe painting is complete. Let me draw it.Selectrow

Required to draw selectrowRowprepaintAndRowpostpaintEvent.

Msdn description:

Rowprepaint

Note: This event is added in. NET Framework 2.0.

This occurs before the datagridviewrow is drawn.

Namespace: system. Windows. Forms

Assembly: system. Windows. Forms (in system. Windows. Forms. dll)

Rowpostpaint

Note: This event is added in. NET Framework 2.0.

Occurs after the datagridviewrow is drawn.

Namespace: system. Windows. Forms

Assembly: system. Windows. Forms (in system. Windows. Forms. dll)

 

There are few identical descriptions,

I will not describe the event parameters one by one. You can go to msdn to view the event parameters.

Go to the topic and start re-painting.

 
 
 
/// <Summary>
 
/// Pre-processing of row re-painting
 
/// </Summary>
 
/// <Param name = "E"> </param>
 
Protected Override VoidOnrowprepaint (maid E)
 
{
 
Base. Onrowprepaint (E );
 
 
// Whether it is selected
 
If(E. State & datagridviewelementstates. Selected) =
 
Datagridviewelementstates. Selected)
 
{
 
// Calculate the size of the selected area
 
IntWidth =This. Columns. getcolumnswidth (maid. Visible) + _ rowheadwidth;
 
 
 
Rectangle rowbounds =NewRectangle (
 
0, E. rowbounds. Top, width,
 
E. rowbounds. Height );
 
 
// Draw the selected background color
 
Using(Lineargradientbrush backbrush =
 
NewLineargradientbrush (rowbounds,
 
Color. greenyellow,
 
E. inheritedrowstyle. forecolor, 90.0f ))
 
{
 
E. Graphics. fillrectangle (backbrush, rowbounds );
 
E. paintcellscontent (rowbounds );
 
E. Handled =True;
 
}
 
 
 
}
 
}

E. State & datagridviewelementstates. SelectedReturns whether the current row is selected.

This. Columns. getcolumnswidth (maid. Visible)Obtains the width of the current visualized column, that is, the width of the selected area.

 

/// <Summary>
 
/// After row re-painting
 
/// </Summary>
 
/// <Param name = "E"> </param>
 
Protected Override VoidOnrowpostpaint (maid E)
 
{
 
Base. Onrowpostpaint (E );
 
IntWidth =This. Columns. getcolumnswidth (maid. Visible) + _ rowheadwidth;
 
Rectangle rowbounds =NewRectangle (
0, E. rowbounds. Top, width, E. rowbounds. Height );
 
 
 
If(This. Currentcelladdress. Y = E. rowindex ){
 
// Set the selected border
 
E. drawfocus (rowbounds,True);
 
}
 
}

 

E. drawfocus (rowbounds,True)Draw borders

Drawing complete, on

 

All right, all done. The custom dview page is drawn.

Below are a few questions that "Xiao Bai" may ask.

1. The painting is ugly: I am not an artist or painter. I just want to tell you how to re-paint and where to re-paint, as for how the painting is more beautiful and the color is better, it is the art of the art.

2. What are lineargradientbrush, professionalcolors, and controlpaint? You can find them on the "tianshu" website by yourself.

3. Benefits: it is hard to say what the benefits are. You just need to learn more. If you really want to find a reason, no matter how your Windows style changes, the datagridview is consistent.

Source codeDownload: full system compatibility with Microsoft

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.