加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

DataGridView中输入错误数据的处理(五)

发布时间:2020-12-16 23:00:12 所属栏目:大数据 来源:网络整理
导读:26. DataGridView 单元格数据错误标签表示 27. DataGridView 单元格内输入值正确性判断 28. DataGridView 单元格输入错误值事件的捕获 26. DataGridView 单元格数据错误标签表示 VB.NET] '(0,0) のセルにエラーアイコンを表示する DataGridView1(0,0).ErrorT

26. DataGridView单元格数据错误标签表示

27. DataGridView单元格内输入值正确性判断

28. DataGridView单元格输入错误值事件的捕获

26. DataGridView单元格数据错误标签表示

VB.NET]

'(0,0)のセルにエラーアイコンを表示する

DataGridView1(0,0).ErrorText = "セルの値を確認してください。"

'インデックスが3の行にエラーアイコンを表示する

DataGridView1.Rows(3).ErrorText = "負の値は入力できません。"

[C#]

//(0,0)のセルにエラーアイコンを表示する

DataGridView1[0,0].ErrorText = "セルの値を確認してください。";

//インデックスが3の行にエラーアイコンを表示する

DataGridView1.Rows[3].ErrorText = "負の値は入力できません。";

在大量单元格需要错误提示时,也可以用CellErrorTextNeededRowErrorTextNeeded事件

[VB.NET]

'CellErrorTextNeededイベントハンドラ

Private Sub DataGridView1_CellErrorTextNeeded(ByVal sender As Object,_

ByVal e As DataGridViewCellErrorTextNeededEventArgs) _

Handles DataGridView1.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 = "負の整数は入力できません。"

End If

End Sub

'RowErrorTextNeededイベントハンドラ

Private Sub DataGridView1_RowErrorTextNeeded(ByVal sender As Object,_

ByVal e As DataGridViewRowErrorTextNeededEventArgs) _

Handles DataGridView1.RowErrorTextNeeded

Dim dgv As DataGridView = CType(sender,DataGridView)

If dgv("Column1",e.RowIndex).Value Is DBNull.Value AndAlso _

dgv("Column2",e.RowIndex).Value Is DBNull.Value Then

e.ErrorText = _

"少なくともColumn1Column2のどちらかには値を入力してください。"

End If

End Sub

[C#]

//CellErrorTextNeededイベントハンドラ

private void DataGridView1_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 = "負の整数は入力できません。";

}

}

//RowErrorTextNeededイベントハンドラ

private void DataGridView1_RowErrorTextNeeded(object sender,

DataGridViewRowErrorTextNeededEventArgs e)

{

DataGridView dgv = (DataGridView)sender;

if (dgv["Column1",e.RowIndex].Value == DBNull.Value &&

dgv["Column2",e.RowIndex].Value == DBNull.Value)

{

e.ErrorText =

"少なくともColumn1Column2のどちらかには値を入力してください。";

}

}

27. DataGridView单元格内输入值正确性判断

[VB.NET]

'CellValidatingイベントハンドラ

Private Sub DataGridView1_CellValidating(ByVal sender As Object,_

ByVal e As DataGridViewCellValidatingEventArgs) _

Handles DataGridView1.CellValidating

Dim dgv As DataGridView = CType(sender,DataGridView)

If dgv.Columns(e.ColumnIndex).Name = "Column1" AndAlso _

e.FormattedValue.ToString() = "" Then

'行にエラーテキストを設定

dgv.Rows(e.RowIndex).ErrorText = "値が入力されていません。"

'入力した値をキャンセルして元に戻すには、次のようにする

'dgv.CancelEdit()

'キャンセルする

e.Cancel = True

End If

End Sub

'CellValidatedイベントハンドラ

Private Sub DataGridView1_CellValidated(ByVal sender As Object,_

ByVal e As DataGridViewCellEventArgs) _

Handles DataGridView1.CellValidated

Dim dgv As DataGridView = CType(sender,DataGridView)

'エラーテキストを消す

dgv.Rows(e.RowIndex).ErrorText = Nothing

End Sub

[C#]

//CellValidatingイベントハンドラ

private void DataGridView1_CellValidating(object sender,

DataGridViewCellValidatingEventArgs e)

{

DataGridView dgv = (DataGridView)sender;

if (dgv.Columns[e.ColumnIndex].Name == "Column1" &&

e.FormattedValue.ToString() == "")

{

//行にエラーテキストを設定

dgv.Rows[e.RowIndex].ErrorText = "値が入力されていません。";

//入力した値をキャンセルして元に戻すには、次のようにする

//dgv.CancelEdit();

//キャンセルする

e.Cancel = true;

}

}

//CellValidatedイベントハンドラ

private void DataGridView1_CellValidated(object sender,

DataGridViewCellEventArgs e)

{

DataGridView dgv = (DataGridView)sender;

//エラーテキストを消す

dgv.Rows[e.RowIndex].ErrorText = null;

}

28. DataGridView单元格输入错误值事件的捕获

[VB.NET]

'DataErrorイベントハンドラ

Private Sub DataGridView1_DataError(ByVal sender As Object,_

ByVal e As DataGridViewDataErrorEventArgs) _

Handles DataGridView1.DataError

If Not (e.Exception Is Nothing) Then

MessageBox.Show(Me,_

String.Format("({0},{1}) のセルでエラーが発生しました。" + _

vbCrLf + vbCrLf + "説明: {2}",_

e.ColumnIndex,e.RowIndex,e.Exception.Message),_

"エラーが発生しました",_

MessageBoxButtons.OK,_

MessageBoxIcon.Error)

End If

End Sub

[C#]

//DataErrorイベントハンドラ

private void DataGridView1_DataError(object sender,

DataGridViewDataErrorEventArgs e)

{

if (e.Exception != null)

{

MessageBox.Show(this,

string.Format("({0},{1}) のセルでエラーが発生しました。/n/n説明: {2}",

e.ColumnIndex,

"エラーが発生しました",

MessageBoxButtons.OK,MessageBoxIcon.Error);

}

}

输入错误值时返回原先数据

[VB.NET]

'DataErrorイベントハンドラ

Private Sub DataGridView1_DataError(ByVal sender As Object,_

ByVal e As DataGridViewDataErrorEventArgs) _

Handles DataGridView1.DataError

e.Cancel = False

End Sub

[C#]

//DataErrorイベントハンドラ

private void DataGridView1_DataError(object sender,

DataGridViewDataErrorEventArgs e)

{

e.Cancel = false;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读