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

DataGridView中输入错误数据的处理(五),第1张

概述26. DataGridView单元格数据错误标签表示 27. DataGridView单元格内输入值正确性判断 28. DataGridView单元格输入错误值事件的捕获    26. DataGridView单元格数据错误标签表示 VB.NET] '(0, 0)のセルにエラーアイコンを表示する DataGridView1(0, 0).ErrorText = "セルの値を確認してください。" '

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;

} 总结

以上是内存溢出为你收集整理的DataGridView中输入错误数据的处理(五)全部内容,希望文章能够帮你解决DataGridView中输入错误数据的处理(五)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1294500.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存