Hello! Can someone help me? I need to get the product of the 2 columns in the same row but my problem is the values of my datagridview come from database and the number for rows is not fixed. Thank you very much and God Bless. :)

Dani AI

Generated

A few practical ways to get a per-row product when your grid is filled from a database. gave a good starting idea, but her example multiplies BillAmount by itself — it should multiply the other column (for example Commission). Depending on where you want the work done, choose one of these options.

Server-side (SQL): simplest and fastest for large result sets — let the database return a computed column.

SELECT BillAmount, Commission, (BillAmount * Commission) AS Product
FROM YourTable;

DataTable expression (compute before binding): add a computed column to the DataTable so the grid shows Product automatically.

' dt is the DataTable you filled
dt.Columns.Add("Product", GetType(Double), "BillAmount * Commission")

Client-side (when users can edit values): recalc the Product cell on edits so it updates immediately.

Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
    If e.RowIndex < 0 Then Return
    Dim row = DataGridView1.Rows(e.RowIndex)
    Dim bill, comm As Double
    Double.TryParse(Convert.ToString(row.Cells("BillAmount").Value), bill)
    Double.TryParse(Convert.ToString(row.Cells("Commission").Value), comm)
    row.Cells("Product").Value = bill * comm
End Sub

Notes: ensure columns are numeric (or convert them) and handle DBNulls. Make the Product column ReadOnly and set formatting (currency/decimals) as needed. For very large datasets prefer server-side computation for performance.

Hi ....
try this code.........


For i = 0 To TempDataset.Tables(0).Rows.Count - 1
Dim Productvalue As Double = 0
Productvalue = Val(TempDataset.Tables(0).Rows(i)("BillAmount").ToString) * Val(TempDataset.Tables(0).Rows(i)("BillAmount").ToString)
PatientDataGridView.Rows.Add(TempDataset.Tables(0).Rows(i)("PatientID").ToString, TempDataset.Tables(0).Rows(i)("PatientName").ToString, _
TempDataset.Tables(0).Rows(i)("BillAmount").ToString, TempDataset.Tables(0).Rows(i)("Commission").ToString, Productvalue)


Next

May I ask, "BillAmount" is for what? Thanks and God Bless. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.