C # create a background image of the datagridview in winform

Source: Internet
Author: User

Yesterday, a friend suddenly asked me how to draw a background image for the datagridview dview under C #. When I used some third-party controls, I saw that they have this function, but I have never had such a requirement, so I tried it.

The first thought was backgroundimage. During the past two days, I was working on the B/S interface. I also thought it was convenient to make the interface, and I had to say CSS. From this point on, WPF or Silverlight is really good, it is a pity that the application of C/S is getting smaller and smaller, except that the game must (of course, a large game) be used for desktop applications.ProgramIn addition, it seems that it is unnecessary to use C/S as a management system. In the enterprise management system, does WPF and Silverlight mean the killing technology in the process?

I also went far away, but soon found that backgroundimage was not working, and looked at its definition:

 
[Browsable (false)] [editorbrowsable (editorbrowsablestate. Never)] public override image backgroundimage {Get; set ;}

Invisible, a property deliberately hidden from development tools, can you expect it to have functionality? Don't give up,CodeIf there is no effect.

So I think of the ultimate Big method: The onpaint method. Generally, in the control, rewrite this method to draw out any shape you want, but in the datagridveiw control, unexpected problems occurred:

1. When the E. Graphics. drawimage () method is called before base. onpaint (E), the part of the data row is transparent, but the background part is still the original color;

2. When the E. Graphics. drawimage () method is called after base. onpaint (e), all the dview s are overwritten by the image. It is also necessary to think about this situation.

Is it true that gridview cannot draw a background? I don't quite believe it, so I went back to msdn (many people don't even read msdn at ordinary times and asked for help if they encountered problems. This is rather bad) and I found a method:

 
Protected virtual void paintbackground (Graphics graphics, rectangle clipbounds, rectangle gridbounds );

The name is awesome. Rewrite it as soon as possible:

 
Protected override void paintbackground (Graphics graphics, rectangle clipbounds, rectangle gridbounds) {graphics. drawimageunscaledandclipped (this. backgroundimage, gridbounds );}

Haha, you don't want me to use backgroundimage. I still have to use it. The backgroundimage is only "hidden", but it is still okay to use it, just to save the background image.

Sure enough, after running the program, the result is displayed, but another problem is that the background of the Data row is transparent. In this way, the data may be affected by the background, and thus cannot be clearly viewed, it is best to add a translucent background to the data row (this is an hour's research process ...) in the end, I found that there is a way to draw the background of a cell. If the cell is drawn, will the data rows be naturally o-intensive?

Protected internal virtual void oncellpainting (maid E );

The final Code confirms:

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. drawing. drawing2d; namespace windowsformsapplication3 {public partial class form1: FORM {public form1 () {initializecomponent (); this. datagridview1.backgroundimage = image. fromfile ("C: \ 2216400.jpg"); this. datag Ridview1.defaultcellstyle. backcolor = color. fromargb (128, color. white); this. datagridview1.defaultcellstyle. selectionbackcolor = color. fromargb (128, color. blue) ;}} public class mydatagrid: datagridview {protected override void paintbackground (Graphics graphics, rectangle clipbounds, rectangle gridbounds) {graphics. drawimageunscaledandclipped (this. backgroundimage, gridbounds);} protected over Ride void oncellpainting (maid e) {If (E. rowindex =-1 | E. columnindex =-1) {return;} 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), selectedcolorbrush = new solidbrush (E. cel Lstyle. selectionbackcolor) {using (PEN gridlinepen = new pen (gridbrush) {If (this. rows [E. rowindex]. selected) {e. graphics. fillrectangle (selectedcolorbrush, E. cellbounds);} else {e. graphics. fillrectangle (backcolorbrush, E. cellbounds);} If (E. value! = NULL) {e. graphics. drawstring (string) E. value, E. cellstyle. font, brushes. black, E. cellbounds. X + 2, E. cellbounds. Y + 2, stringformat. genericdefault) ;}} E. handled = true ;}}}}

:

This code can only illustrate one idea. If you want to use it, you must make some effort, for example, to prevent jitter During Scaling and cell borders. I just made a prototype and won't go further.

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.