How Multiply data in columns of DataGridView

Hi People,

I'am using one DataGridView, and i'am multiply 2 cells of this, e after update this in one Access Data Base.

In the screen this datas is correct, but after update, the cells used in multiply is empty in the Data Base.

My code for multiply:

If e.ColumnIndex = Me.DataGridView1.Columns(6).Index _
AndAlso (e.Value IsNot Nothing) Then

With Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
one = e.Value.ToString
End With
End If

If e.ColumnIndex = Me.DataGridView1.Columns(8).Index _
AndAlso (e.Value IsNot Nothing) Then

With Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
two = e.Value.ToString
End With
End If

If e.ColumnIndex = Me.DataGridView1.Columns(9).Index _
AndAlso (e.Value IsNot Nothing) Then

With Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
If one = String.Empty Then
one = "0"
End If
If two = String.Empty Then
two = "0"
End If
e.Value = one * two
End With
End If

[1107 byte] By [LuisMCesar] at [2008-1-10]
# 1

Luis M Cesar,

According to your question on multiply DataGridView columns and update to the database, I would like to provide you the suggestions as follows:

1. Please try to handle DataGridView.CellParsing event that occurs when a cell leaves edit mode if the cell value has been modified. When you handle the CellParsing event, you can convert the value yourself or you can customize the default conversion.

The following code example shows how to handle the CellParsing event.

Code Snippet

' Handling CellParsing allows one to accept user input, then map it to a different

' internal representation.

Private Sub dataGridView1_CellParsing(ByVal sender As Object, _

ByVal e As DataGridViewCellParsingEventArgs) _

Handles dataGridView1.CellParsing

If Me.dataGridView1.Columns(e.ColumnIndex).Name = _

"Release Date" Then

If e IsNot Nothing Then

If e.Value IsNot Nothing Then

Try

' Map what the user typed into UTC.

e.Value = _

DateTime.Parse(e.Value.ToString()).ToUniversalTime()

' Set the ParsingApplied property to

' Show the event is handled.

e.ParsingApplied = True

Catch ex As FormatException

' Set to false in case another CellParsing handler

' wants to try to parse this DataGridViewCellParsingEventArgs instance.

e.ParsingApplied = False

End Try

End If

End If

End If

End Sub

2. Update database from DataGridView issue: Please try to use DataAdapter.Update method that calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified DataSet from a DataTable named "Table." Then refresh the data source of DataGridView.

The following article provide you the further help: Updating Data Sources with DataAdapters

3. Please take a look at the following threads with the similar questions

datagridView CellFormatting and cellParsing for formatted values and stored values

The code snippet is provided by Rinaldi and I try to convert to VB.NET in order to help you better:

Code Snippet

Private bCellValueChanged As Boolean = False

'Declared boolean so that CellValueChanged event does not run in loop because the values are reset in this event.

Private Sub dataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)

If dataGridView1.Columns(e.ColumnIndex).Name = "BHTemp" AndAlso bCellValueChanged = False Then

If dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString() Is Nothing Then

bCellValueChanged = True

dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0

bCellValueChanged = False

Else

f = Double.Parse(dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString())

bCellValueChanged = True

dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = Temperature_Parse_dgv(f)

' Temperature_Parse_dgv converts value from Celsius to Kelvin.

bCellValueChanged = False

End If

End If

End Sub


Hope that can help you.

BrunoYu-MSFT at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic IDE...