基本信息
源码名称:DataGridView显示进度条.rar
源码大小:0.04M
文件格式:.rar
开发语言:C#
更新时间:2019-08-17
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
DataGridView显示进度条 DataGridView显示进度条 DataGridView显示进度条

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.IO;
using System.Threading;
using System.Globalization;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Reflection;

namespace WinTest
{
    public partial class frmMain : Form
    {        
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        private void InitializeComponent()
        {
            this.dgv = new System.Windows.Forms.DataGridView();
            this.clFileName = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.clProcess = new System.Windows.Forms.DataGridViewTextBoxColumn();
            ((System.ComponentModel.ISupportInitialize)(this.dgv)).BeginInit();
            this.SuspendLayout();
            // 
            // dgv
            // 
            this.dgv.AllowUserToAddRows = false;
            this.dgv.AllowUserToDeleteRows = false;
            this.dgv.AllowUserToOrderColumns = true;
            this.dgv.AllowUserToResizeColumns = false;
            this.dgv.AllowUserToResizeRows = false;
            this.dgv.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            this.dgv.BackgroundColor = System.Drawing.Color.White;
            this.dgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.clFileName,
            this.clProcess});
            this.dgv.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dgv.Location = new System.Drawing.Point(0, 0);
            this.dgv.Name = "dgv";
            this.dgv.RowHeadersVisible = false;
            this.dgv.RowTemplate.Height = 23;
            this.dgv.Size = new System.Drawing.Size(326, 255);
            this.dgv.TabIndex = 0;
            this.dgv.CellPainting  = new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.dgv_CellPainting);
            // 
            // FileName
            // 
            this.clFileName.HeaderText = "文件名";
            this.clFileName.Name = "FileName";
            this.clFileName.ReadOnly = true;
            // 
            // Process
            // 
            this.clProcess.DefaultCellStyle.Format = "0%";
            this.clProcess.DefaultCellStyle.SelectionBackColor = System.Drawing.Color.White;
            this.clProcess.HeaderText = "进度";
            this.clProcess.Name = "Process";
            this.clProcess.ReadOnly = true;
            // 
            // frmMain
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(326, 255);
            this.Controls.Add(this.dgv);
            this.Name = "frmMain";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "frmMain";
            ((System.ComponentModel.ISupportInitialize)(this.dgv)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private DataGridView dgv;
        private DataGridViewTextBoxColumn clFileName;
        private DataGridViewTextBoxColumn clProcess;

        public frmMain()
        {
            InitializeComponent();
        }

        void dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            lock (dgv)
            {
                if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
                {
                    if (dgv.Columns["Process"].Index == e.ColumnIndex)
                    {
                        Rectangle rg, fillrg;
                        using (Brush gridBrush = new SolidBrush(Color.Black), WhiteBrush = new SolidBrush(Color.White), backColorBrush = new SolidBrush(Color.FromArgb(204, 255, 0)))
                        {
                            e.PaintBackground(e.CellBounds, true);
                            using (Pen gridLinePen = new Pen(gridBrush))
                            {
                                // 画矩形框
                                rg = e.CellBounds;
                                rg.Inflate(-6, -5);
                                e.Graphics.FillRectangle(WhiteBrush, rg);
                                e.Graphics.DrawRectangle(gridLinePen, rg);
                                // 填充矩形进度条
                                decimal width = Convert.ToDecimal(rg.Width - 1);
                                width = width * Convert.ToDecimal(e.Value);
                                fillrg = new Rectangle(rg.X   1, rg.Y   1, Convert.ToInt32(width), rg.Height - 1);
                                e.Graphics.FillRectangle(backColorBrush, fillrg);
                            }
                            e.Graphics.DrawString(e.FormattedValue.ToString(), e.CellStyle.Font, gridBrush, rg.X   rg.Width / 2 - 9, rg.Y);
                            e.Handled = true;
                        }
                    }
                }
                Application.DoEvents();
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            dgv.Rows.Add(1);
            dgv.Rows[0].Cells[0].Value = "C#高级编程.pdf";
            new Thread(() =>
                {
                    for (decimal i = 0; i <= 100; i  )
                    {
                        this.dgv.Rows[0].Cells["Process"].Value = i / 100;
                        Thread.Sleep(100);
                    }
                }).Start();
            base.OnLoad(e);
        }
    }
}