DataGridView控件用法合集(五)

来源:互联网 发布:不用网络的枪战游戏 编辑:程序博客网 时间:2024/05/16 08:29
近期将DataGridView常用的一些用法做了一个整理。为防止页面过长,现分批贴出来,此为第五部分。

DataGridView中输入错误数据的处理

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 = _

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

    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 =

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

    }

}

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, e.RowIndex, e.Exception.Message),

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

            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;

}

原创粉丝点击