■■■

2016年4月1日金曜日

DataGridViewのセル値によってセルスタイルを変更する方法

DataGridViewのセル値によってセルスタイルを変更する方法
VB.NET
Private Sub dgv1_CellFormatting(ByVal sender As Object, _
ByVal e As DataGridViewCellFormattingEventArgs) _
Handles dgv1.CellFormatting
Dim dgv As DataGridView = CType(sender, DataGridView)

If dgv.Columns(e.ColumnIndex).Name = "Column1" AndAlso _
TypeOf e.Value Is Integer Then
Dim
val As Integer = CInt(e.Value)
If val < 0 Then
e.CellStyle.BackColor = Color.Yellow
Else If val = 0 Then
e.CellStyle.BackColor = Color.Red
End If
End If
End Sub
C#
private void dgv1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;

if (dgv.Columns[e.ColumnIndex].Name == "Column1" && e.Value is int)
{
int val = (int)e.Value;
if (val < 0)
{
e.CellStyle.BackColor = Color.Yellow;
}
else if (val == 0)
{
e.CellStyle.BackColor = Color.Red;
}
}
}













■■■