C # rewrite datagridview_c# tutorial

Source: Internet
Author: User

  This example for you to share the C # rewrite DataGridView instance code for your reference, the specific content as follows

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Drawing;
Using System.Windows.Forms;
Using System.Drawing.Drawing2D; Namespace CRD. winui.editors {public class DataGridView:System.Windows.Forms.DataGridView {private bool _cellcoloronchange=fa
    Lse
    Private Color cell_color = Color.yellow;
    private bool Shifouhuasanjiao = true;
    Private Color Color_grid = Color.FromArgb (236, 233, 216);
    BOOL Click = FALSE; Public DataGridView () {this. SetStyle (Controlstyles.doublebuffer |
    Controlstyles.allpaintinginwmpaint, True); } protected override void Oncreatecontrol () {this.
      Enableheadersvisualstyles = false; This.
      Columnheadersdefaultcellstyle.backcolor = Color.FromArgb (236, 233, 216); This.
      Columnheadersborderstyle = datagridviewheaderborderstyle.raised; This.
      Columnheadersheight = 20; This.
      Columnheadersheightsizemode = datagridviewcolumnheadersheightsizemode.disableresizing; This.Columnheadersdefaultcellstyle.alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; This.
      Columnheadersdefaultcellstyle.forecolor = System.Drawing.SystemColors.WindowText; This.
      Columnheadersdefaultcellstyle.selectionbackcolor = System.Drawing.SystemColors.Highlight; This.
      Columnheadersdefaultcellstyle.selectionforecolor = System.Drawing.SystemColors.HighlightText; This.
      Rowheadersdefaultcellstyle.alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; This.
      Rowheadersdefaultcellstyle.backcolor = Color.FromArgb (236, 233, 216); This.
      Rowheadersdefaultcellstyle.forecolor = System.Drawing.SystemColors.WindowText; This.
      Rowheadersborderstyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Raised; This.
      Defaultcellstyle.selectionbackcolor = Color.darkblue; This.
      Defaultcellstyle.selectionforecolor = Color.darkslateblue; This. Rowheaderswidthsizemode = Datagridviewrowheaderswidthsizemode.disableresizing; This. Gridcolor = color.silver;//form click Color form line Color this.
      BackgroundColor = System.Drawing.SystemColors.Window; This.
      BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; This.
      Allowusertoordercolumns = true; This.
      AutoGenerateColumns = true; Base.
    Oncreatecontrol ();
    } Color DefaultColor; The color to move to the cell protected override void Oncellmousemove (DataGridViewCellMouseEventArgs e) {base.
      Oncellmousemove (e); try {if (_cellcoloronchange) Rows[e.rowindex].
      Defaultcellstyle.backcolor = Cell_color; The current color protected override void Oncellmouseenter (DATAGRIDV) is saved when the catch (Exception) {}}//enters the cell. Iewcelleventargs e) {base.
      Oncellmouseenter (e); try {if (_cellcoloronchange) DefaultColor = Rows[e.rowindex].
      Defaultcellstyle.backcolor; The Restore color protected override void Oncellmouselea when the catch (Exception) {}}//Leftve (DataGridViewCellEventArgs e) {base.
      Oncellmouseleave (e); try {if (_cellcoloronchange) Rows[e.rowindex].
      Defaultcellstyle.backcolor = DefaultColor; The catch (Exception) {}} is public bool Cellcoloronchange {get {return _
      Cellcoloronchange;
      } set {_cellcoloronchange = value;
      } public Color Defaultcolorset {get {return cell_color;
      } set {Cell_color = value;
      } public bool Shifouhua_sanjiao {get {return shifouhuasanjiao;
      } set {Shifouhuasanjiao = value;
      } public Color Content_grid_color {get {return color_grid;
      } set {Color_grid = value; } private void InitializeComponent () {(System.ComponentModel.ISupportInitialize) (this).
BeginInit ();      This.
      SuspendLayout (); DataGridView////this.
       
      Rowtemplate.height = 17; ((System.ComponentModel.ISupportInitialize) (this)).
      EndInit (); This.
    ResumeLayout (FALSE);  }//rowpostpaint protected override void Onrowpostpaint (Datagridviewrowpostpainteventargs e) {if
      (Shifouhuasanjiao) {using (SolidBrush B = new SolidBrush (Color.Black)) {image image = Global::crd.
          WinUI.Properties.Resources. Untitled _1;
          E.graphics.drawstring ("►", E.inheritedrowstyle.font, B, e.rowbounds.location.x + 5, E.ROWBOUNDS.LOCATION.Y + 4);
          e.graphics.drawimageunscaled (image, E.rowbounds.location.x + 1, E.ROWBOUNDS.LOCATION.Y + 2, 8, 13); if (click) if (E.rowindex = = this. Currentrow.index) {e.graphics.drawimageunscaled (image, E.rowbounds.location.x + 1, E.ROWBOUNDS.LOCATION.Y + 2)
          , 8, 13); {}} Base.
    Onrowpostpaint (e); } protected override void Onrowprepaint (Datagridviewrowprepainteventargs e) {if (Shifouhuasanjiao {using (SolidBrush B = new SolidBrush (Color.Black)) {image image = Global::crd.
          WinUI.Properties.Resources. Untitled _1;
           E.graphics.drawstring ("►", E.inheritedrowstyle.font, B, e.rowbounds.location.x + 5, E.ROWBOUNDS.LOCATION.Y + 4);
        e.graphics.drawimageunscaled (image, E.rowbounds.location.x + 1, E.ROWBOUNDS.LOCATION.Y + 2, 8, 13); } base.
    Onrowprepaint (e); } protected override void Oncellclick (DataGridViewCellEventArgs e) {if (E.rowindex > -1&&this.
      Currentrow.index = = e.rowindex) {click = true; Base.
    Oncellclick (e); } protected override void Oncellpainting (DataGridViewCellPaintingEventArgs e) {base.
       
      Oncellpainting (e);
SolidBrush B = New SolidBrush (Color.FromArgb (236, 233, 216));      Pen Whitepen = new Pen (color_grid, 1); if (E.columnindex = = 1 && e.rowindex = = 1) {using (LinearGradientBrush brush = new LinearGradient Brush (E.cellbounds, Color.gray, Color.gray, lineargradientmode.forwarddiagonal)) {e.graphics.
          FillRectangle (b, e.cellbounds);
          Rectangle border = e.cellbounds; Border.
          Offset (New Point (-1,-1));
        E.graphics.drawrectangle (Pens.gray, border);
        } e.paintcontent (E.cellbounds);
      E.handled = true; else if (E.rowindex = = 1) {//header row using (LinearGradientBrush brush = new Lineargradientbrus H (e.cellbounds, Color.silver, Color.silver, lineargradientmode.vertical)) {E.graphics.fillrec
          Tangle (b, e.cellbounds);
          Rectangle border = e.cellbounds; Border.
          Offset (New Point (-1,-1));
          E.graphics.drawrectangle (Pens.silver, border); E.graphics.drawrectangle (PEns. Black, border. X + 1, border. Y + 1, border. Width-1, border.
        HEIGHT-1);
        } e.paintcontent (E.cellbounds);
      E.handled = true; else if (E.columnindex = = 1) {//Header column using (LinearGradientBrush brush = new LINEARGRADIENTB Rush (E.cellbounds, Color.silver, Color.silver, lineargradientmode.horizontal)) {E .
          Graphics.FillRectangle (b, e.cellbounds);
          Rectangle border = e.cellbounds; Border.
          Offset (New Point (-1,-1));
          E.graphics.drawrectangle (Pens.silver, border); E.graphics.drawrectangle (Pens.black, border. X+1,border. Y+1,border. Width-1,border.
          HEIGHT-1);
        E.graphics.drawstring ("", font,b,e.cellbounds.x,e.cellbounds.y);
        } e.paintcontent (E.cellbounds);
      E.handled = true;
        else {//color.fromargb (193, 193, 193) Rectangle border = e.cellbounds; Border.
 
       Offset (New Point (-1,-1)); E.graphics.drawrectangle (Whitepen, border); }
    }
  }
}

The above is the entire content of this article, I hope to help you learn.

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.