惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
Recent Announcements
Recent Announcements
L
LangChain Blog
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
J
Java Code Geeks
博客园_首页
Jina AI
Jina AI
美团技术团队
H
Help Net Security
MyScale Blog
MyScale Blog
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
S
SegmentFault 最新的问题

博客园 - nametmp

【转】Resource与Resx的区别 - nametmp - 博客园 异步调用过程 Winform用匿名方法新建线程的方法 enum枚举用法笔记 对C#委托的理解 对using的新认识 视频文件信息读取器 琐碎记录 【转贴】GridView里面的HYPERLINK数据邦定 【转贴】数据邦定语法 - nametmp - 博客园 TableAdapter、Dataset与BindingSource的关系 webform 可视化在gridview中加入其它控件 WinForm 加入登陆窗体(c#) 远程删除Kaspersky(被Kaspersky搞了一天) SqlServer2000登陆角色的具体权限 终于用sqlcmd连上sqlserver2005express了 sqlserver操作摘录 [转]SQL Server 2005中的SQLCMD工具使用 [转]sqlserver2005(Express版)的配置
winform 中的 datagridview 添加 progressbar列 和 calendar 列
nametmp · 2007-08-17 · via 博客园 - nametmp

【转载】
步骤:
1、在项目新建类文件,并装入附件的源码,编译一次。
2、添加datagridview,属性-杂项-colums-选定某一列-改变columntype即可。

【DataGridViewProgressColumn】

//---------------------------------------------------------------------
//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.
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace Test
{
    
public class DataGridViewProgressColumn : DataGridViewImageColumn
    
{
        
public DataGridViewProgressColumn()
        
{
            CellTemplate 
= new DataGridViewProgressCell();
        }

    }


    
class DataGridViewProgressCell : DataGridViewImageCell
    
{
        
// Used to make custom cell consistent with a DataGridViewImageCell
        static Image emptyImage;
        
static DataGridViewProgressCell()
        
{
            emptyImage 
= new Bitmap(11, 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)
        
{            
            
if (value==null)
            
{
                
throw new ArgumentNullException();                
            }

            
int progressVal = (int)value;
            
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)
            
{
                
// Draw the progress bar and the text
                g.FillRectangle(new SolidBrush(Color.FromArgb(163189242)), 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);
            }

        }

    }

}

【CalendarColumn】

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace Test
{
    
public class CalendarColumn : DataGridViewColumn
    
{
        
private bool showUpDown = true;
        
public bool ShowUpDown
        
{
            
get
            
{
                
return showUpDown;
            }

            
set
            
{
                showUpDown 
= value;
            }

        }

        
public CalendarColumn()
            : 
base(new CalendarCell())
        
{
        }

        
public override DataGridViewCell CellTemplate
        
{
            
get
            
{
                
return base.CellTemplate;
            }

            
set
            
{
                
if (value != null &&
                
!value.GetType().IsAssignableFrom(typeof(CalendarCell)))
                
{
                    
throw new InvalidCastException("Must be a CalendarCell");
                }

                
base.CellTemplate = value;
            }

        }

        
private void InitializeComponent()
        
{
        }

    }


    
public class CalendarCell : DataGridViewTextBoxCell
    
{
        
public CalendarCell()
            : 
base()
        
{
        }

        
public override void InitializeEditingControl(int rowIndex, object
        initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        
{
            
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            CalendarEditingControl ctl 
=
            DataGridView.EditingControl 
as CalendarEditingControl;
            ctl.Text 
= this.Value.ToString();
        }

        
public override Type EditType
        
{
            
get
            
{
                
return typeof(CalendarEditingControl);
            }

        }

        
public override Type ValueType
        
{
            
get
            
{
                
return typeof(DateTime);
            }

        }

        
public override object DefaultNewRowValue
        
{
            
get
            
{
                
return DateTime.Now;
            }

        }

    }


    
class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
    
{
        DataGridView dataGridView;
        
private bool valueChanged = false;
        
int rowIndex;
        
public CalendarEditingControl()
        
{
        }

        
public object EditingControlFormattedValue
        
{
            
get
            
{
                
return this.Value.ToLongDateString();
            }

            
set
            
{
                String newValue 
= value as String;
                
if (newValue != null)
                
{
                    
this.Value = DateTime.Parse(newValue);
                }

            }

        }

        
public object GetEditingControlFormattedValue(
        DataGridViewDataErrorContexts context)
        
{
            
return EditingControlFormattedValue;
        }

        
public void ApplyCellStyleToEditingControl(
        DataGridViewCellStyle dataGridViewCellStyle)
        
{
            
this.Font = dataGridViewCellStyle.Font;
            
this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
            
this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
        }

        
public int EditingControlRowIndex
        
{
            
get
            
{
                
return rowIndex;
            }

            
set
            
{
                rowIndex 
= value;
            }

        }

        
public bool EditingControlWantsInputKey(
        Keys key, 
bool dataGridViewWantsInputKey)
        
{
            
switch (key & Keys.KeyCode)
            
{
                
case Keys.Left:
                
case Keys.Up:
                
case Keys.Down:
                
case Keys.Right:
                
case Keys.Home:
                
case Keys.End:
                
case Keys.PageDown:
                
case Keys.PageUp:
                    
return true;
                
default:
                    
return false;
            }

        }

        
public void PrepareEditingControlForEdit(bool selectAll)
        
{
        }

        
public bool RepositionEditingControlOnValueChange
        
{
            
get
            
{
                
return false;
            }

        }

        
public DataGridView EditingControlDataGridView
        
{
            
get
            
{
                
return dataGridView;
            }

            
set
            
{
                dataGridView 
= value;
            }

        }

        
public bool EditingControlValueChanged
        
{
            
get
            
{
                
return valueChanged;
            }

            
set
            
{
                valueChanged 
= value;
            }

        }

        
public Cursor EditingPanelCursor
        
{
            
get
            
{
                
return base.Cursor;
            }

        }

        
protected override void OnValueChanged(EventArgs eventargs)
        
{
            valueChanged 
= true;
            
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            
base.OnValueChanged(eventargs);
        }

    }


}

补充:
在ProgressBar的paint函数中,要对value和progressval附加处理,
            
            if (value==null)
            {               
                value = (object)0;
            }
            int progressVal;
            try
            {
                progressVal = Convert.ToInt16(value);
            }
            catch
            {
                progressVal = 0;
            }