Home Ask Login Register

Developers Planet

Your answer is one click away!

Macukadam February 2016

Combining two values from different columns into one column in Datagridwiev

I am using MS SQL Server and Visual Basic.

I want to combine a calculated column and another column to add units to the calculation. (for example "200 m2")

DgwNewLayout.Columns(5).DefaultCellStyle.Format = "0.00 " & "'" & DgwNewLayout.CurrentRow.Cells(9).Value & "'"

This code does that but only for first entered unit? How can I do this for each column?


VV5198722 February 2016

You could use CellFormatting event.

Private WithEvents DgwNewLayout As DataGridView

Private Sub DgwNewLayout_CellFormatting(sender As Object,
    e As DataGridViewCellFormattingEventArgs
) Handles DgwNewLayout.CellFormatting

    If e.ColumnIndex = 5 Then
        e.Value = CDbl(e.Value).ToString("0.00") &
            " " & CStr(DgwNewLayout.Rows(e.RowIndex).Cells(9).Value)
        e.FormattingApplied = True
    End If
End Sub

As Dom Sinclair says, this could have poor performance.

Post Status

Asked in February 2016
Viewed 2,813 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life