■■■

2016年4月2日土曜日

DataGridViewの各セルにプログレスバーを表示する方法

DataGridViewの各セルにプログレスバーを表示する方法
VB.NET
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class DataGridViewProgressBarColumn
Inherits DataGridViewTextBoxColumn

Public Sub New()
Me.CellTemplate = New DataGridViewProgressBarCell()
End Sub

Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase
.CellTemplate
End Get
Set
(ByVal value As DataGridViewCell)
If Not TypeOf value Is DataGridViewProgressBarCell Then
' Error
End If
MyBase
.CellTemplate = value
End Set
End Property


Public Property Maximum() As Integer
Get
Return CType
(Me.CellTemplate, DataGridViewProgressBarCell).Maximum
End Get
Set
(ByVal value As Integer)
If Me.Maximum = value Then
Return
End If

CType(Me.CellTemplate, DataGridViewProgressBarCell).Maximum = value
If Me.DataGridView Is Nothing Then
Return
End If
Dim
rowCount As Integer = Me.DataGridView.RowCount
Dim i As Integer
For
i = 0 To rowCount - 1
Dim r As DataGridViewRow = Me.DataGridView.Rows.SharedRow(i)
CType(r.Cells(Me.Index), DataGridViewProgressBarCell).Maximum = _
value
Next i
End Set
End Property


Public Property Mimimum() As Integer
Get
Return CType
(Me.CellTemplate, DataGridViewProgressBarCell).Mimimum
End Get
Set
(ByVal value As Integer)
If Me.Mimimum = value Then
Return
End If

CType(Me.CellTemplate, DataGridViewProgressBarCell).Mimimum = value
If Me.DataGridView Is Nothing Then
Return
End If
Dim
rowCount As Integer = Me.DataGridView.RowCount
Dim i As Integer
For
i = 0 To rowCount - 1
Dim r As DataGridViewRow = Me.DataGridView.Rows.SharedRow(i)
CType(r.Cells(Me.Index), DataGridViewProgressBarCell).Mimimum = _
value
Next i
End Set
End Property
End Class


Public Class DataGridViewProgressBarCell
Inherits DataGridViewTextBoxCell

Public Sub New()
Me.maximumValue = 100
Me.mimimumValue = 0
End Sub

Private
maximumValue As Integer

Public Property
Maximum() As Integer
Get
Return Me
.maximumValue
End Get
Set
(ByVal value As Integer)
Me.maximumValue = value
End Set
End Property

Private
mimimumValue As Integer

Public Property
Mimimum() As Integer
Get
Return Me
.mimimumValue
End Get
Set
(ByVal value As Integer)
Me.mimimumValue = value
End Set
End Property


Public Overrides ReadOnly Property ValueType() As Type
Get
Return GetType
(Integer)
End Get
End Property


Public Overrides ReadOnly Property DefaultNewRowValue() As Object
Get
Return
0
End Get
End Property


Public Overrides Function Clone() As Object
Dim
cell As DataGridViewProgressBarCell = _
CType(MyBase.Clone(), DataGridViewProgressBarCell)
cell.Maximum = Me.Maximum
cell.Mimimum = Me.Mimimum
Return cell
End Function

Protected Overrides Sub
Paint(ByVal graphics As Graphics, _
ByVal clipBounds As Rectangle, _
ByVal cellBounds As Rectangle, _
ByVal rowIndex As Integer, _
ByVal cellState As DataGridViewElementStates, _
ByVal value As Object, _
ByVal formattedValue As Object, _
ByVal errorText As String, _
ByVal cellStyle As DataGridViewCellStyle, _
ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, _
ByVal paintParts As DataGridViewPaintParts)

Dim intValue As Integer = 0
If TypeOf value Is Integer Then
intValue = CInt(value)
End If
If
intValue < Me.mimimumValue Then
intValue = Me.mimimumValue
End If
If
intValue > Me.maximumValue Then
intValue = Me.maximumValue
End If
Dim rate As Double = CDbl(intValue - Me.mimimumValue) / _
(Me.maximumValue - Me.mimimumValue)

If (paintParts And DataGridViewPaintParts.Border) = _
DataGridViewPaintParts.Border Then
Me
.PaintBorder(graphics, clipBounds, cellBounds, _
cellStyle, advancedBorderStyle)
End If

Dim borderRect As Rectangle = Me.BorderWidths(advancedBorderStyle)
Dim paintRect As New Rectangle(cellBounds.Left + borderRect.Left, _
cellBounds.Top + borderRect.Top, _
cellBounds.Width - borderRect.Right, _
cellBounds.Height - borderRect.Bottom)

Dim isSelected As Boolean = _
((cellState And DataGridViewElementStates.Selected) = _
DataGridViewElementStates.Selected)
Dim bkColor As Color
If isSelected AndAlso _
(paintParts And DataGridViewPaintParts.SelectionBackground) = _
DataGridViewPaintParts.SelectionBackground Then
bkColor = cellStyle.SelectionBackColor
Else
bkColor = cellStyle.BackColor
End If

If (paintParts And DataGridViewPaintParts.Background) = _
DataGridViewPaintParts.Background Then
Dim
backBrush As New SolidBrush(bkColor)
Try
graphics.FillRectangle(backBrush, paintRect)
Finally
backBrush.Dispose()
End Try
End If


paintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top)
paintRect.Width -= cellStyle.Padding.Horizontal
paintRect.Height -= cellStyle.Padding.Vertical

If (paintParts And DataGridViewPaintParts.ContentForeground) = _
DataGridViewPaintParts.ContentForeground Then
If
ProgressBarRenderer.IsSupported Then
ProgressBarRenderer.DrawHorizontalBar(graphics, paintRect)
Dim barBounds As New Rectangle(paintRect.Left + 3, _
paintRect.Top + 3, _
paintRect.Width - 4, _
paintRect.Height - 6)
barBounds.Width = CInt(Math.Round((barBounds.Width * rate)))
ProgressBarRenderer.DrawHorizontalChunks(graphics, barBounds)
Else
graphics.FillRectangle(Brushes.White, paintRect)
graphics.DrawRectangle(Pens.Black, paintRect)
Dim barBounds As New Rectangle(paintRect.Left + 1, _
paintRect.Top + 1, _
paintRect.Width - 1, _
paintRect.Height - 1)
barBounds.Width = CInt(Math.Round((barBounds.Width * rate)))
graphics.FillRectangle(Brushes.Blue, barBounds)
End If
End If


If Me.DataGridView.CurrentCellAddress.X = Me.ColumnIndex AndAlso _
Me.DataGridView.CurrentCellAddress.Y = Me.RowIndex AndAlso _
(paintParts And DataGridViewPaintParts.Focus) = _
DataGridViewPaintParts.Focus AndAlso _
Me.DataGridView.Focused Then

Dim focusRect As Rectangle = paintRect
focusRect.Inflate(-3, -3)
ControlPaint.DrawFocusRectangle(graphics, focusRect)
End If

If (paintParts And DataGridViewPaintParts.ContentForeground) = _
DataGridViewPaintParts.ContentForeground Then
Dim txt As String = String.Format("{0}%", Math.Round((rate * 100)))
Dim flags As TextFormatFlags = _
TextFormatFlags.HorizontalCenter Or _
TextFormatFlags.VerticalCenter
Dim fColor As Color = cellStyle.ForeColor
paintRect.Inflate(-2, -2)
TextRenderer.DrawText( _
graphics, txt, cellStyle.Font, paintRect, fColor, flags)
End If

If (paintParts And DataGridViewPaintParts.ErrorIcon) = _
DataGridViewPaintParts.ErrorIcon AndAlso _
Me.DataGridView.ShowCellErrors AndAlso _
Not String.IsNullOrEmpty(errorText) Then

Dim iconBounds As Rectangle = _
Me.GetErrorIconBounds(graphics, cellStyle, rowIndex)
iconBounds.Offset(cellBounds.X, cellBounds.Y)
Me.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText)
End If
End Sub
End Class
Dim pbColumn As New DataGridViewProgressBarColumn()
pbColumn.DataPropertyName = "col1"
dgv1.Columns.Add(pbColumn)
C#
using System;
using System.Drawing;
using System.Windows.Forms;

public class DataGridViewProgressBarColumn : DataGridViewTextBoxColumn
{
public DataGridViewProgressBarColumn()
{
this.CellTemplate = new DataGridViewProgressBarCell();
}

public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (!(value is DataGridViewProgressBarCell))
{
// Error
}
base.CellTemplate = value;
}
}

public int Maximum
{
get
{
return ((DataGridViewProgressBarCell)this.CellTemplate).Maximum;
}
set
{
if (this.Maximum == value)
return;
((DataGridViewProgressBarCell)this.CellTemplate).Maximum =
value;
if (this.DataGridView == null)
return;
int rowCount = this.DataGridView.RowCount;
for (int i = 0; i < rowCount; i++)
{
DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
((DataGridViewProgressBarCell)r.Cells[this.Index]).Maximum =
value;
}
}
}

public int Mimimum
{
get
{
return ((DataGridViewProgressBarCell)this.CellTemplate).Mimimum;
}
set
{
if (this.Mimimum == value)
return;
((DataGridViewProgressBarCell)this.CellTemplate).Mimimum =
value;
if (this.DataGridView == null)
return;
int rowCount = this.DataGridView.RowCount;
for (int i = 0; i < rowCount; i++)
{
DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
((DataGridViewProgressBarCell)r.Cells[this.Index]).Mimimum =
value;
}
}
}
}

public class DataGridViewProgressBarCell : DataGridViewTextBoxCell
{
public DataGridViewProgressBarCell()
{
this.maximumValue = 100;
this.mimimumValue = 0;
}

private int maximumValue;
public int Maximum
{
get
{
return this.maximumValue;
}
set
{
this.maximumValue = value;
}
}

private int mimimumValue;
public int Mimimum
{
get
{
return this.mimimumValue;
}
set
{
this.mimimumValue = value;
}
}

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

public override object DefaultNewRowValue
{
get
{
return 0;
}
}

public override object Clone()
{
DataGridViewProgressBarCell cell =
(DataGridViewProgressBarCell)base.Clone();
cell.Maximum = this.Maximum;
cell.Mimimum = this.Mimimum;
return cell;
}

protected override void Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds,
int rowIndex, DataGridViewElementStates cellState,
object value, object formattedValue, string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
int intValue = 0;
if (value is int)
intValue = (int)value;
if (intValue < this.mimimumValue)
intValue = this.mimimumValue;
if (intValue > this.maximumValue)
intValue = this.maximumValue;
double rate = (double)(intValue - this.mimimumValue) /
(this.maximumValue - this.mimimumValue);

if ((paintParts & DataGridViewPaintParts.Border) ==
DataGridViewPaintParts.Border)
{
this.PaintBorder(graphics, clipBounds, cellBounds,
cellStyle, advancedBorderStyle);
}

Rectangle borderRect = this.BorderWidths(advancedBorderStyle);
Rectangle paintRect = new Rectangle(
cellBounds.Left + borderRect.Left,
cellBounds.Top + borderRect.Top,
cellBounds.Width - borderRect.Right,
cellBounds.Height - borderRect.Bottom);

bool isSelected =
(cellState & DataGridViewElementStates.Selected) ==
DataGridViewElementStates.Selected;
Color bkColor;
if (isSelected &&
(paintParts & DataGridViewPaintParts.SelectionBackground) ==
DataGridViewPaintParts.SelectionBackground)
{
bkColor = cellStyle.SelectionBackColor;
}
else
{
bkColor = cellStyle.BackColor;
}
        if ((paintParts & DataGridViewPaintParts.Background) ==
DataGridViewPaintParts.Background)
{
using (SolidBrush backBrush = new SolidBrush(bkColor))
{
graphics.FillRectangle(backBrush, paintRect);
}
}

paintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);
paintRect.Width -= cellStyle.Padding.Horizontal;
paintRect.Height -= cellStyle.Padding.Vertical;

if ((paintParts & DataGridViewPaintParts.ContentForeground) ==
DataGridViewPaintParts.ContentForeground)
{
if (ProgressBarRenderer.IsSupported)
{
ProgressBarRenderer.DrawHorizontalBar(graphics, paintRect);
Rectangle barBounds = new Rectangle(
paintRect.Left + 3, paintRect.Top + 3,
paintRect.Width - 4, paintRect.Height - 6);
barBounds.Width = (int)Math.Round(barBounds.Width * rate);
ProgressBarRenderer.DrawHorizontalChunks(graphics, barBounds);
}
else
{
graphics.FillRectangle(Brushes.White, paintRect);
graphics.DrawRectangle(Pens.Black, paintRect);
Rectangle barBounds = new Rectangle(
paintRect.Left + 1, paintRect.Top + 1,
paintRect.Width - 1, paintRect.Height - 1);
barBounds.Width = (int)Math.Round(barBounds.Width * rate);
graphics.FillRectangle(Brushes.Blue, barBounds);
}
}

if (this.DataGridView.CurrentCellAddress.X == this.ColumnIndex &&
this.DataGridView.CurrentCellAddress.Y == this.RowIndex &&
(paintParts & DataGridViewPaintParts.Focus) ==
DataGridViewPaintParts.Focus &&
this.DataGridView.Focused)
{
Rectangle focusRect = paintRect;
focusRect.Inflate(-3, -3);
ControlPaint.DrawFocusRectangle(graphics, focusRect);
}

if ((paintParts & DataGridViewPaintParts.ContentForeground) ==
DataGridViewPaintParts.ContentForeground)
{
string txt = string.Format("{0}%", Math.Round(rate * 100));

TextFormatFlags flags = TextFormatFlags.HorizontalCenter |
TextFormatFlags.VerticalCenter;
Color fColor = cellStyle.ForeColor;
paintRect.Inflate(-2, -2);
TextRenderer.DrawText(graphics, txt, cellStyle.Font,
paintRect, fColor, flags);
}

if ((paintParts & DataGridViewPaintParts.ErrorIcon) ==
DataGridViewPaintParts.ErrorIcon &&
this.DataGridView.ShowCellErrors &&
!string.IsNullOrEmpty(errorText))
{
Rectangle iconBounds = this.GetErrorIconBounds(
graphics, cellStyle, rowIndex);
iconBounds.Offset(cellBounds.X, cellBounds.Y);
this.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText);
}
}
}
DataGridViewProgressBarColumn pbColumn =
new DataGridViewProgressBarColumn();
pbColumn.DataPropertyName = "col1";
dgv1.Columns.Add(pbColumn);
■■■