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?
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(D sCrate1, "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.
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(D
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.
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(By Val sender As Object, ByVal e As System.Web.UI.WebControls. DataGridIt emEventArg s) Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Item And e.Item.ItemType = ListItemType.AlternatingIt em 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.Coun t - 1).FindControl("txtCrateTo tal"), 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
if it is correct you can use something like that
Private Sub DataGrid1_ItemDataBound(By
If e.Item.ItemType = ListItemType.Item And e.Item.ItemType = ListItemType.AlternatingIt
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.
Next
Dim tb As TextBox
tb = CType(e.Item.Cells(e.Item.
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
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
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q787q
this should help you
B..G
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
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
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(ByVa l sender As Object, ByVal e As System.Data.DataRowChangeE ventArgs) Handles m_dtCrate.RowChanging
If e.Action = DataRowAction.Add Then
TextBox2.Text = System.Convert.ToString(Sy stem.Conve rt.ToDecim al(TextBox 2.Text) + e.Row.Item("YourColumnName ToBeTotall ed"))
End If
End Sub
I hope this throws some light on the issue.
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(ByVa
If e.Action = DataRowAction.Add Then
TextBox2.Text = System.Convert.ToString(Sy
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)
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(
Next
txtlSum.Text = Convert.ToString(decSum)
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
Thanks
Create a view with desired values and then run the code for the view instead of the table
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.
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.
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.DefaultViewMana ger.Create DataView(M yDataSet.T ables(Some Index))
dv1.RowFilter = "ID=" & txtBox1.text 'or whatever
MyControl.DataSource = dv1
dv1 =MyDataSet.DefaultViewMana
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
ASKER
I'll Try it again. Thanks!
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?
The DataGrid is filled using "DataGrid1.SetDataBinding(
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(gr id.DataSou rce, grid.DataMember), CurrencyManager)
Dim d As Double = 0
Dim i As Integer
While i < cm.Count
d += System.Convert.ToDouble(CT ype(cm.Lis t(i), DataRowView)(col))
i += 1
End While
Return d
End Function
Private Function SumColumn(ByVal grid As GridDataBoundGrid, ByVal col As String) As Double
Dim cm As CurrencyManager = CType(Me.BindingContext(gr
Dim d As Double = 0
Dim i As Integer
While i < cm.Count
d += System.Convert.ToDouble(CT
i += 1
End While
Return d
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.