DataGridView..::.CellValueChanged 事件

DataGridView . . :: .CellValueChanged 事件

更新:2007 年 11 月

在单元格的值更改时发生。

命名空间:  System.Windows.Forms
程序集:  System.Windows.Forms(在 System.Windows.Forms.dll 中)

Visual Basic(声明)
Public Event CellValueChanged As DataGridViewCellEventHandler
Visual Basic (用法)
Dim instance As DataGridView
Dim handler As DataGridViewCellEventHandler

AddHandler instance.CellValueChanged, handler
C#
public event DataGridViewCellEventHandler CellValueChanged
Visual C++
public:
 event DataGridViewCellEventHandler^ CellValueChanged {
    void add (DataGridViewCellEventHandler^ value);
    void remove (DataGridViewCellEventHandler^ value);
}
J#
/** @event */
public void add_CellValueChanged (DataGridViewCellEventHandler value)
/** @event */
public void remove_CellValueChanged (DataGridViewCellEventHandler value)
JScript
JScript 不支持事件。

DataGridView..::.CellValueChanged 事件在用户指定的值提交时发生,用户指定的值通常是在焦点离开单元格时提交。

但是,对于复选框单元格,您通常希望立即处理更改。要在单击单元格时提交更改,必须处理 DataGridView..::.CurrentCellDirtyStateChanged 事件。在处理程序中,如果当前单元格是复选框单元格,将调用 DataGridView..::.CommitEdit 方法并传入 Commit 值。

当单元格值发生改变时,控件中的行不会自动进行排序。要在用户修改单元格时对控件进行排序,请在 CellValueChanged 事件处理程序中调用 Sort 方法。

有关处理事件的更多信息,请参见使用事件

下面的代码示例演示如何使用 CellValueChanged 事件来更新 DataGridView 的余额列中的值。此示例摘自 SelectionChanged 事件中提供的一个更大的示例。

Visual Basic
Private Sub CellValueChanged(ByVal sender As Object, _
    ByVal e As DataGridViewCellEventArgs) _
    Handles DataGridView1.CellValueChanged

    ' Update the balance column whenever the values of any cell changes.
    UpdateBalance()
End Sub

Private Sub RowsRemoved(ByVal sender As Object, _
    ByVal e As DataGridViewRowsRemovedEventArgs) _
    Handles DataGridView1.RowsRemoved

    ' Update the balance column whenever rows are deleted.
    UpdateBalance()
End Sub

Private Sub UpdateBalance()
    Dim counter As Integer
    Dim balance As Integer
    Dim deposit As Integer
    Dim withdrawal As Integer

    ' Iterate through the rows, skipping the Starting Balance Row.
    For counter = 1 To (DataGridView1.Rows.Count - 2)
        deposit = 0
        withdrawal = 0
        balance = Integer.Parse(DataGridView1.Rows(counter - 1) _
            .Cells("Balance").Value.ToString())

        If Not DataGridView1.Rows(counter) _
            .Cells("Deposits").Value Is Nothing Then

            ' Verify that the cell value is not an empty string.
            If Not DataGridView1.Rows(counter) _
                .Cells("Deposits").Value.ToString().Length = 0 Then
                deposit = Integer.Parse(DataGridView1.Rows(counter) _
                    .Cells("Deposits").Value.ToString())
            End If
        End If

        If Not DataGridView1.Rows(counter) _
            .Cells("Withdrawals").Value Is Nothing Then
            If Not DataGridView1.Rows(counter) _
                .Cells("Withdrawals").Value.ToString().Length = 0 Then
                withdrawal = Integer.Parse(DataGridView1.Rows(counter) _
                    .Cells("Withdrawals").Value.ToString())
            End If
        End If

        DataGridView1.Rows(counter).Cells("Balance").Value = _
            (balance + deposit + withdrawal).ToString()
    Next
End Sub

private void DataGridView1_CellValueChanged(
    object sender, DataGridViewCellEventArgs e)
{
    // Update the balance column whenever the value of any cell changes.
    UpdateBalance();
}

private void DataGridView1_RowsRemoved(
    object sender, DataGridViewRowsRemovedEventArgs e)
{
    // Update the balance column whenever rows are deleted.
    UpdateBalance();
}

private void UpdateBalance()
{
    int counter;
    int balance;
    int deposit;
    int withdrawal;

    // Iterate through the rows, skipping the Starting Balance row.
    for (counter = 1; counter < (DataGridView1.Rows.Count - 1);
        counter++)
    {
        deposit = 0;
        withdrawal = 0;
        balance = int.Parse(DataGridView1.Rows[counter - 1]
            .Cells["Balance"].Value.ToString());

        if (DataGridView1.Rows[counter].Cells["Deposits"].Value != null)
        {
            // Verify that the cell value is not an empty string.
            if (DataGridView1.Rows[counter]
                .Cells["Deposits"].Value.ToString().Length != 0)
            {
                deposit = int.Parse(DataGridView1.Rows[counter]
                    .Cells["Deposits"].Value.ToString());
            }
        }

        if (DataGridView1.Rows[counter].Cells["Withdrawals"].Value != null)
        {
            if (DataGridView1.Rows[counter]
                .Cells["Withdrawals"].Value.ToString().Length != 0)
            {
                withdrawal = int.Parse(DataGridView1.Rows[counter]
                    .Cells["Withdrawals"].Value.ToString());
            }
        }
        DataGridView1.Rows[counter].Cells["Balance"].Value =
            (balance + deposit + withdrawal).ToString();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值