Adding column values displayed on a DataGrid

T

Teo

Hey guys,
I have the following code that displays DB values into a datagrid.
I want to add each of the columns in the datagrid and print the total on a
textbox I will add on the bottom.
How can I add the values that are displayed in a single column on a
datagrid?
Thanks much,
Teo

Here's the code:
Try

Dim conn2 As New MySqlConnection(strConn)
Dim strSQL2 As String
strSQL2 = "SELECT l_date as DATE, l_reg as REGISTRATION,
l_actype as TYPE, l_dep as DEPARTURE, l_arr as ARRIVAL, "
strSQL2 = strSQL2 + "l_remarks as REMARKS, l_landings as LDGS,
l_ifrapp as IFR_APP, l_seland as S_E_LAND, l_meland as M_E_LAND, l_night as
NIGHT,"
strSQL2 = strSQL2 + "l_simifr as SIM_IFR, l_actifr as ACT_IFR,
l_fsim as FSIM, l_xcountry as X_COUNTRY,"
strSQL2 = strSQL2 + "l_cfi as CFI, l_dual as DUAL_I, l_pic as
PIC, "
strSQL2 = strSQL2 + "l_total as TOTAL FROM logbook WHERE s_id =
'" + id + "' ORDER BY l_date ASC"
Dim dataadapter As New MySqlDataAdapter(strSQL2, conn2)
Dim dataset As DataSet = New DataSet


dataadapter.Fill(dataset, "Logbook")

dg.DataSource = dataset.Tables(0)
dg.ScrollBars = ScrollBars.Both
dg.AllowUserToAddRows = False
dg.AllowUserToDeleteRows = False

Any suggestions are greatly appreciated.
 
C

Cor Ligthert[MVP]

Teo,

In my idea, will your code not work, the datagrid has no
"AllowUserToAddRows".

Try it with a DataGridView, however that is completeley different from a
DataGrid

Cor
 
T

Teo

Hi Cor,
The issue is that I don't want User to add rows since I am just reading this
set of fields for viewing only purposes.
I want to place a text box on the bottom outside the datagridview (which is
the one I am using in this code) that will add each of the column values
that is returned.

That's what I am trying to figure out.

Thanks much anyways,
Teo
 
C

Cor Ligthert[MVP]

Teo,

Maybe can your state your question than first correct. It is so useless as
this answer can not be searched on internet because you have stated it
completely wrong, and then the answers will give wrong impressions about the
use of the DataGrid.

You could have asked as well, "Is there any reason why my users cannot drink
melk?

Maybe will a complete new message wich correct describes your problem help
you.

Cor
 
J

Jack Jackson

Hey guys,
I have the following code that displays DB values into a datagrid.
I want to add each of the columns in the datagrid and print the total on a
textbox I will add on the bottom.
How can I add the values that are displayed in a single column on a
datagrid?
Thanks much,
Teo

Here's the code:
Try

Dim conn2 As New MySqlConnection(strConn)
Dim strSQL2 As String
strSQL2 = "SELECT l_date as DATE, l_reg as REGISTRATION,
l_actype as TYPE, l_dep as DEPARTURE, l_arr as ARRIVAL, "
strSQL2 = strSQL2 + "l_remarks as REMARKS, l_landings as LDGS,
l_ifrapp as IFR_APP, l_seland as S_E_LAND, l_meland as M_E_LAND, l_night as
NIGHT,"
strSQL2 = strSQL2 + "l_simifr as SIM_IFR, l_actifr as ACT_IFR,
l_fsim as FSIM, l_xcountry as X_COUNTRY,"
strSQL2 = strSQL2 + "l_cfi as CFI, l_dual as DUAL_I, l_pic as
PIC, "
strSQL2 = strSQL2 + "l_total as TOTAL FROM logbook WHERE s_id =
'" + id + "' ORDER BY l_date ASC"
Dim dataadapter As New MySqlDataAdapter(strSQL2, conn2)
Dim dataset As DataSet = New DataSet


dataadapter.Fill(dataset, "Logbook")

dg.DataSource = dataset.Tables(0)
dg.ScrollBars = ScrollBars.Both
dg.AllowUserToAddRows = False
dg.AllowUserToDeleteRows = False

Any suggestions are greatly appreciated.

You clearly don't want to add each column since some columns, and in
fact most columns, don't look like they are numbers (DATE, Type,
REMARKS, X_COUNTRY, etc.).

Assuming you want to sum one or more columns, you could do this
(untested code):

Dim totalCount as Integer = 0
For Each dr As DataRow In dataset.Tables(0).Rows
totalCount += Convert.ToInt32(dr("TOTAL"))
Next

txtTotal.Text = totalCount.ToString()

If you are using VS2008 you could probably do this in one LINQ
statement.
 
Top