Total the qty column in a datagrid

DavidT_macktool

Well-known member
Joined
Oct 21, 2004
Messages
502
Location
Indiana
Programming Experience
3-5
In an Inventory dataset I have an integer field "quantity". This field contains positive values when pieces are added and negative values when pieces are used. I am displaying the records in a datagrid and I want the "total pieces on hand" displayed in a label on the form.

How do I total the "Quantity" column of the datagrid and end up with an integer variable I can assign the Label.text to?

I am using an SQLDataset. Should I try to do the summing in the Select statement, HOW?
Should I loop through the rows of the datagrid?

Help on the internet is directed towards displaying totals in the datagrid footer, I dont want that.

I need the total to be updated when a new datagrid record is added.

Any help would be appriciated.
Thanks for looking.
 
DavidT_macktool said:
I am using an SQLDataset. Should I try to do the summing in the Select statement, HOW?
Should I loop through the rows of the datagrid?
A select would probably be the easyest way:
SELECT SUM[Qyt] FROM Where ever

You could go throught the datadrid like this;
dim i,total as int
while i < datagrid.visableRowCount
total += datagrid.item(i, qtycolumn)
i +=1
end while

TPM
 
Thanks for the reply

I have experimented with the select and can't really reference the value summed directly to databind.
I went this way and it works, but you have to update the textbox after updating the dataset.

sumTotal = DsInventory1.Tables(0).Compute("Sum(Quantity)", String.Empty)

lblTotQty2.Text = sumTotal

thanks,
 
in your dataadpter add a field to the dataset that's the sum of the qty column, then simply exclude it from the datagrid and databind a label to the new field
 
Back
Top