Link to home
Start Free TrialLog in
Avatar of WesGoad
WesGoad

asked on

Total DataGrid Column and display in A TextBox

I converted a VB 6.0 prgram to VB .net. The 6.0 code would allow me to total a flexgrid column and Display the total in a text box. All the other code in the program uses this value. I've looked at all the other solutions and the best I could do is to total the entire dataset, not the data displayed in the DataGrid. Can anyone help?
Avatar of pwyll_2x
pwyll_2x

You can walk the datagrid and total from there.

For Each item As DataGridItem In DataGrid1.Items
   Dim myCol As Object = item.Cells(2)
   ' etc.
Next

Obviously it requires that you know the layout of the datagrid and how it maps to your data...  but what you want to do is possible.

P.
Avatar of WesGoad

ASKER

This is where I run into problems. I'm fairly new to .net. When I try to use the code that I've found on Forum sites, I don't have the same properties as everyone else. I Don't get "DataGridItem" as a property.

I should also give you some more detail. I changed the FlexGrid to a DataGrid. I am also using a SQL Database to load the grid. The grid is filled based on a transation number using this code:

        With Me.DsCrate1
            DataGrid1.SetDataBinding(DsCrate1, "Package.TransNo")
        End With

The procedure called will add up the totals in the "Quantity" column and display it in the textBox "txtCrateTotal". The Quantity Column is the third Column in the DataGrid.

It looks like I might need some walking through myself.
Ah...   DataGridItem is a TYPE not a property.   And you would get it by adding the following import:

Imports System.Web.UI.WebControls

As well as a reference to the System.Web assembly, if you don't already have one.

i'm assuming that you talk about Web application
if it is correct you can use something like that

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        If e.Item.ItemType = ListItemType.Item And e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim i As Integer
            Dim count As Integer

            For i = 0 To e.Item.Cells.Count - 2
                count = count + CInt(e.Item.Cells(i).Text.Trim())
            Next

            Dim tb As TextBox
            tb = CType(e.Item.Cells(e.Item.Cells.Count - 1).FindControl("txtCrateTotal"), TextBox)
            If Not tb Is Nothing Then
                tb.Text = count.ToString()
            End If
        End If
    End Sub

if you bind all the data to the datagrid and you have an additional column for the textbox control txtCreateTotal
then this should be for you

HTH
B..G
Avatar of WesGoad

ASKER

I'm sorry~! I forgot to state it was a form and not a Web APP.
take a look at this link
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q787q
this should help you

B..G
Avatar of WesGoad

ASKER

I couldn't get anything from the link. I could add a column to the DataGrid, but it still doesn't get me a value to put into a textBox.

Thanks
Wes, if you get the data from a db, can you change the sql statement for that the "Total" column will be a sum of all others?
if you can do this you don't have to write some code to do it

B..G
One of the Powerful features of a DataTable that is not used is the Events on a DataTable.

The DataTable has events that can be used in conjunction with the actions on the DataGrid. The following are the events on a DataTable.

RowChanged, RowChanging, ColumnChanged, ColumnChanging, RowDeleted, RowDeleting

For your problem, please use the RowChanged as shown below:
Step 1: Declare a DataTable withevents
eg: Private WithEvents m_dtCrate as new DataTable

Step 2: Fill this DataTable with the data using SqlDataAdapter.Fill

Step 3: In Set DataBinding use this DataTable as DataSource

Step 4: Add the event Handler code as shown below
  Private Sub m_dtCrate_RowChanging(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles m_dtCrate.RowChanging
        If e.Action = DataRowAction.Add Then
            TextBox2.Text = System.Convert.ToString(System.Convert.ToDecimal(TextBox2.Text) + e.Row.Item("YourColumnNameToBeTotalled"))
        End If

    End Sub

I hope this throws some light on the issue.
Dim dr As DataRow
        Dim decSum As Decimal
        For Each dr In ds.Tables(t).Rows '<-- Change t to the talbe you want (t=0,1,2...)
            ' *** You may wnat to determine what to do whith nulls here
            decSum += Convert.ToDecimal(dr.Item(c)) '<-- Change c to the column you like (0,1,2...)
        Next
       txtlSum.Text = Convert.ToString(decSum)
Avatar of WesGoad

ASKER

The last code sample sent from "iboutchkine" works except it totals the entire table instead of just the displayed values in the grid that come from a specified transaction number.

Thanks
Create a view with desired values and then run the code for the view instead of the table
Avatar of WesGoad

ASKER

Correct me if I'm wrong, but the example previded from "Mogun" is for a web app. I'm having trouble getting anything to work with my form.
Avatar of WesGoad

ASKER

I've tired all suggestions with no luck. The one suggestion to set the dataview to the values I need seems most plausible, except I don't know how to filter the dataview based on a value in a textbox, which is from another table "Package". @%^#$^#&
Am I thinking "inside the box" and there is a whole other approach to this situation? All other forum sites have not been able to come up with a solution to this problem using a Form App. In VB 6.0, all I'd have to do it get the data using TextMatrix property.
Dim dv1 as DataView
dv1 =MyDataSet.DefaultViewManager.CreateDataView(MyDataSet.Tables(SomeIndex))
dv1.RowFilter = "ID=" & txtBox1.text  'or whatever
MyControl.DataSource = dv1
The solution i suggested will work with both winforms and webforms. You should declare a DataTable withevents and use it
Avatar of WesGoad

ASKER

I'll Try it again. Thanks!
Avatar of WesGoad

ASKER

I declared the DataTable, filled it, and tried to bind the data to the DataGrid, but I cannot get it to filter based on the value from a textbox(transaction Number).
The DataGrid is filled using "DataGrid1.SetDataBinding(DsCrate1, "Package.Transno")", where Package.Transno is the value of the current transaction number from the Package table. This loads the datagrid with only the values for the current transaction number. I can't see that the datatable will give me this data using "DataGrid1.SetDataBinding(m_dtCrate, "Package.Transno")" and setting the datacource for the Datagrid to the table. Am I doing something wrong?

Avatar of WesGoad

ASKER

I found a solution that worked. I purchased a third party table add-on and was able to add the displayed columns using the following code.

    Private Function SumColumn(ByVal grid As GridDataBoundGrid, ByVal col As String) As Double
        Dim cm As CurrencyManager = CType(Me.BindingContext(grid.DataSource, grid.DataMember), CurrencyManager)
        Dim d As Double = 0
        Dim i As Integer
        While i < cm.Count
            d += System.Convert.ToDouble(CType(cm.List(i), DataRowView)(col))
            i += 1
        End While
        Return d
    End Function
ASKER CERTIFIED SOLUTION
Avatar of amp072397
amp072397

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial