You must Sign In to post a response.
  • Category: Visual Studio

    Alternative to Cell End Edit in Datagridview ?

    Hi....

    I am using windows application (VB.NET). I have a form with datagridview with columns qty , unit price and cost . The Cost column is qty * unit price column. When i enter unit cost in unit price column i wrote this calculation in cell end edit event for datagridview but when mouse left the focus only the event do calculations.. Is there any other event to do calculation in data gridiview so that calculation is correct.

    Thanks and Regards
    Sankar
  • #765587
    Hi Sankar,

    Ofcourse you will not get the changed value in CellEndEdit event as the changes are not committed yet. In your case, you get the value in the third step because the changed value is committed by then.

    If you want to get the changed value then subscribe to CellValueChanged event.

    dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
    In a sample program, I changed a cell value from 1 to 3. See how the events are fired and the value change happening.

    CellLeave - 1
    CellValidating - 1
    CellValueChanged - 3
    CellValidated - 3
    CellEndEdit - 3

  • #765594
    Cell Value Changed event is not working .Is there any other event

  • #765602
    Hi,
    Try this:
    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles TaskGridView.EditingControlShowing
    If Me.TaskGridView.CurrentCell.ColumnIndex = 3 And Not e.Control Is Nothing Then
    Dim tb As TextBox = CType(e.Control, TextBox)
    AddHandler tb.KeyPress, AddressOf Cell_KeyPress
    End If
    End Sub

    Private Sub Cell_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    If (TaskGridView.CurrentCell.ColumnIndex = 3) Then
    Dim cell1 As Integer = Convert.ToInt32(TaskGridView.CurrentRow.Cells(0).Value)
    Dim cell2 As Integer = Char.ConvertFromUtf32(Convert.ToInt32(e.KeyChar))
    If (cell1.ToString() <> "" And cell2.ToString() <> "") Then
    TaskGridView.CurrentRow.Cells(4).Value = cell1 + cell2
    End If
    e.Handled = False
    End If
    End Sub

  • #765606
    Dim cell2 As Integer = Char.ConvertFromUtf32(Convert.ToInt32(e.KeyChar)) it will calculate as one character only suppose i type 12 it will first take 1 then it will multiply with 1 and then it will multiply by 2 but not 12

  • #765611
    I think You should use CellValidating event for this, Occurs when a cell loses input focus, enabling content validation.
    see below snippet

    private void dataGridView1_CellValidating(object sender,
    DataGridViewCellValidatingEventArgs e)
    {
    dataGridView1.Rows[e.RowIndex].ErrorText = "";
    int newInteger;

    // Don't try to validate the 'new row' until finished
    // editing since there
    // is not any point in validating its initial value.
    if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
    if (!int.TryParse(e.FormattedValue.ToString(),
    out newInteger) || newInteger < 0)
    {
    e.Cancel = true;
    dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a non-negative integer";
    }
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765625
    Hi,
    Yes CellEndEdit fires when you done with editing.
    That's why I had suggested you to place a code inside EditingControlShowing and as per my code it will take every inputed character and calculate cost.


  • Sign In to post your comments