zoukankan      html  css  js  c++  java
  • DataGridView功能扩展

    • 添加了ProgressBar列功能
    • Row标题添加DataGridView行序号
    • 奇偶行间变
    • 控件编辑状态下立即提交
    • 选中行变色功能

    using System;
    using System.Windows.Forms;
    using System.Drawing;
    using System.ComponentModel;

    namespace TestDataGridView
    {
    class MyDataGridView:System.Windows.Forms.DataGridView
    {
    SolidBrush solidBrush;
    //Button saveButton;
    //Button openButton;

    public MyDataGridView()
    {
    solidBrush = new SolidBrush(this.RowHeadersDefaultCellStyle.ForeColor);

    this.ColumnHeadersVisible = true;
    //saveButton = new Button();
    //saveButton.Size = new Size(this.ColumnHeadersHeight - 2, 18);
    //saveButton.Location = new Point(0, 1);
    //this.Controls.Add(saveButton);

    //openButton = new Button();
    //openButton.Size = new Size(this.ColumnHeadersHeight - 2, 18);
    //openButton.Location = new Point(20, 1);
    //this.Controls.Add(openButton);

    }
    protected override void OnCellClick(DataGridViewCellEventArgs e)
    {
    base.OnCellClick(e);
    for (int i = 0; i < this.Rows.Count; )
    {
    if (i == e.RowIndex)
    {
    this.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.LightGreen;
    }
    else
    {
    if (i % 2 == 0)
    this.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(234, 254, 255);
    else
    this.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.White;
    }
    i += 1;

    }

    }
    protected override void OnRowsAdded(DataGridViewRowsAddedEventArgs e)
    {
    base.OnRowsAdded(e);
    //颜色渐变
    for (int i = 0; i < this.Rows.Count; )
    {
    if (i % 2 == 0)
    this.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(234, 254, 255);
    else
    this.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.White;
    i += 1;
    }

    }
    protected override void OnCurrentCellDirtyStateChanged(EventArgs e)
    {
    base.OnCurrentCellDirtyStateChanged(e);
    if (this.IsCurrentCellDirty)
    {
    this.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
    }
    protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
    {
    base.OnRowPostPaint(e);
    e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, solidBrush, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);
    }
    }
    }
    //---------------------------------------------------------------------
    //THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
    //KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    //IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
    //PARTICULAR PURPOSE.
    //---------------------------------------------------------------------
    namespace DataGridViewProgress
    {
    public class DataGridViewProgressColumn : DataGridViewImageColumn
    {
    public DataGridViewProgressColumn()
    {
    CellTemplate = new DataGridViewProgressCell();
    }
    }
    }
    namespace DataGridViewProgress
    {
    class DataGridViewProgressCell : DataGridViewImageCell
    {
    // Used to make custom cell consistent with a DataGridViewImageCell
    static Image emptyImage;
    static DataGridViewProgressCell()
    {
    emptyImage = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    }
    public DataGridViewProgressCell()
    {
    this.ValueType = typeof(int);
    }
    // Method required to make the Progress Cell consistent with the default Image Cell.
    // The default Image Cell assumes an Image as a value, although the value of the Progress Cell is an int.
    protected override object GetFormattedValue(object value,
    int rowIndex, ref DataGridViewCellStyle cellStyle,
    TypeConverter valueTypeConverter,
    TypeConverter formattedValueTypeConverter,
    DataGridViewDataErrorContexts context)
    {
    return emptyImage;
    }

    protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    {
    int progressVal;
    if (value != null)
    progressVal = (int)value;
    else
    progressVal = 0;


    float percentage = ((float)progressVal / 100.0f); // Need to convert to float before division; otherwise C# returns int which is 0 for anything but 100%.
    Brush backColorBrush = new SolidBrush(cellStyle.BackColor);
    Brush foreColorBrush = new SolidBrush(cellStyle.ForeColor);
    // Draws the cell grid
    base.Paint(g, clipBounds, cellBounds,
    rowIndex, cellState, value, formattedValue, errorText,
    cellStyle, advancedBorderStyle, (paintParts & ~DataGridViewPaintParts.ContentForeground));
    if (percentage > 0.0 || System.Math.Abs( percentage-0.0)<float.Epsilon)
    {
    // Draw the progress bar and the text
    g.FillRectangle(new SolidBrush(Color.FromArgb(163, 189, 242)), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4);
    g.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, cellBounds.X + 6, cellBounds.Y + 2);
    }
    else
    {
    // draw the text
    //if (this.DataGridView.CurrentRow.Index == rowIndex)
    // g.DrawString(progressVal.ToString() + "%", cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 6, cellBounds.Y + 2);
    //else
    g.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, cellBounds.X + 6, cellBounds.Y + 2);
    }
    }
    }
    }
        
    作者:wanglei_wan
        
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Redis详解
    Linux常用命令
    sqlserver 游标写法
    Flask总结
    HTTPS协议
    Django REST framework 简介
    Cookie和session
    利用rest-framework编写逻辑关系
    vuex
    jQuery和Bootstrap的优点
  • 原文地址:https://www.cnblogs.com/because/p/2316096.html
Copyright © 2011-2022 走看看