■■■

2016年3月31日木曜日

DataGridViewでErrorアイコンを表示する方法

DataGridViewでErrorアイコンを表示する方法
VB.NET
dgv1(0, 0).ErrorText = "セルエラー発生"
dgv1.Rows(3).ErrorText = "行エラー発生"
C#
dgv1[0, 0].ErrorText = "セルエラー発生";
dgv1.Rows[3].ErrorText = "行エラー発生";
CellErrorTextNeededイベントまたはRowErrorTextNeededイベントを活用する方法
VB.NET
Private Sub dgv1_CellErrorTextNeeded(ByVal sender As Object, _
ByVal e As DataGridViewCellErrorTextNeededEventArgs) _
Handles dgv1.CellErrorTextNeeded
Dim dgv As DataGridView = CType(sender, DataGridView)
Dim cellVal As Object = dgv(e.ColumnIndex, e.RowIndex).Value
If TypeOf cellVal Is Integer AndAlso CInt(cellVal) < 0 Then
e.ErrorText = "0以上で入力してください"
End If
End Sub


Private Sub dgv1_RowErrorTextNeeded(ByVal sender As Object, _
ByVal e As DataGridViewRowErrorTextNeededEventArgs) _
Handles dgv1.RowErrorTextNeeded
Dim dgv As DataGridView = CType(sender, DataGridView)
If dgv("
col1", e.RowIndex).Value Is DBNull.Value AndAlso _
dgv("col2", e.RowIndex).Value Is DBNull.Value Then
e.ErrorText = _
"少なくとも
col1col2の一方は必須です"
End If
End Sub
C#
private void dgv1_CellErrorTextNeeded(object sender,
DataGridViewCellErrorTextNeededEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
object cellVal = dgv[e.ColumnIndex, e.RowIndex].Value;
if (cellVal is int && ((int)cellVal) < 0)
{
e.ErrorText = "
0以上で入力してください";
}
}

private void dgv1_RowErrorTextNeeded(object sender,
DataGridViewRowErrorTextNeededEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv["
col1", e.RowIndex].Value == DBNull.Value &&
dgv["Column2", e.RowIndex].Value == DBNull.Value)
{
e.ErrorText =
"
少なくともcol1col2の一方は必須です";
}
}

























■■■