• 学习之路二十一:设置DataGridView中的按钮为Disable


    这个问题折腾很久了,网上找了半天了,结果问一个同事,同事给了一个地址让我看看,最后根据上面的介绍做好了,再次记录一下,o(∩_∩)o 哈哈

    地址如下:http://msdn.microsoft.com/en-us/library/ms171619.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

    代码如下:

     1     public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn
     2     {
     3         public DataGridViewDisableButtonColumn()
     4         {
     5             this.CellTemplate = new DataGridViewDisableButtonCell();
     6         }
     7     }
     8 
     9 
    10     public class DataGridViewDisableButtonCell : DataGridViewButtonCell
    11     {
    12         private bool enabledValue;
    13 
    14         public bool Enabled
    15         {
    16             get
    17             {
    18                 return enabledValue;
    19             }
    20             set
    21             {
    22                 enabledValue = value;
    23             }
    24         }
    25 
    26         public string CommadText { get; set; }
    27 
    28         // Override the Clone method so that the Enabled property is copied. 
    29         public override object Clone()
    30         {
    31             DataGridViewDisableButtonCell cell = (DataGridViewDisableButtonCell)base.Clone();
    32             cell.Enabled = this.Enabled;
    33             return cell;
    34         }
    35 
    36         // By default, enable the button cell. 
    37         public DataGridViewDisableButtonCell()
    38         {
    39             this.enabledValue = true;
    40         }
    41 
    42         protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
    43                                                 DataGridViewElementStates elementState, object value,
    44                                                 object formattedValue, string errorText, DataGridViewCellStyle cellStyle,
    45                                                 DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    46         {
    47            
    48             if (!this.enabledValue)
    49             {
    50                 // Draw the cell background, if specified. 
    51                 if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
    52                 {
    53                     SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);
    54                     graphics.FillRectangle(cellBackground, cellBounds);
    55                     cellBackground.Dispose();
    56                 }
    57 
    58                 // Draw the cell borders, if specified. 
    59                 if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
    60                 {
    61                     PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
    62                 }
    63 
    64                 // Calculate the area in which to draw the button.
    65                 Rectangle buttonArea = cellBounds;
    66                 Rectangle buttonAdjustment = this.BorderWidths(advancedBorderStyle);
    67                 buttonArea.X += buttonAdjustment.X;
    68                 buttonArea.Y += buttonAdjustment.Y;
    69                 buttonArea.Height -= buttonAdjustment.Height;
    70                 buttonArea.Width -= buttonAdjustment.Width;
    71 
    72                 // Draw the disabled button.                
    73                 ButtonRenderer.DrawButton(graphics, buttonArea, PushButtonState.Disabled);
    74 
    75                 // Draw the disabled button text.  
    76                 if (this.FormattedValue is String)
    77                 {
    78                     TextRenderer.DrawText(graphics, (string)this.FormattedValue, this.DataGridView.Font, buttonArea, SystemColors.GrayText);
    79                 }
    80             }
    81             else
    82             {
    83                 // The button cell is enabled, so let the base class  
    84                 // handle the painting. 
    85                 base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
    86             }
    87         }
    88     }

    在此记录一下!

    已同步至:程序猿个人文章目录索引

  • 相关阅读:
    oracle 如何查看oracle数据库版本
    oracle 拼接字符串的两种方式
    ibatis 批量更新(二)
    ibatis中#和$如何当作字符使用?
    SVM核技巧的经典解释
    Koch 分形,海岸线,雪花
    Java程序猿笔试面试之String
    一步一步在Windows下搭建React Native Android开发环境
    IOS是否在项目中存在,所使用的反射那点事
    图的深度搜索和广度搜索
  • 原文地址:https://www.cnblogs.com/yangcaogui/p/2853601.html
Copyright © 2020-2023  润新知