Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use calculated field in Datagrid

Status
Not open for further replies.

JPimp

Technical User
Mar 27, 2007
79
US
I am not even sure if this is possible, I am pulling in data from a DB, populating 2 text fields, just so I can calculate the difference between the two, and then I want to display the difference in the datagrid. This is what I have so far:

Code:
Dim DataReader As OleDbDataReader

        DataGrid1.Visible = True

        Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmdSelect)

        Dim myDataSet = New DataSet
        Dim myDataSet2 = New DataSet

        myDA.Fill(myDataSet, "EQ_METER_READING")
        myDA.Fill(myDataSet2, "WO_PM_SCHEDULE")

        Dim Row As DataRow

        If myDataSet.Tables("EQ_METER_READING").Rows.Count > 0 Then

            Row = myDataSet.Tables("EQ_METER_READING").Rows(0)

        End If

        If Row.Item("READING") Is DBNull.Value Then
            READING.Text = String.Empty
        Else
            READING.Text = Row.Item("READING")

        End If

        If myDataSet2.Tables("WO_PM_SCHEDULE").Rows.Count > 0 Then

            Row = myDataSet2.Tables("WO_PM_SCHEDULE").Rows(0)

        End If

        If Row.Item("NEXT_EXE_UNITS") Is DBNull.Value Then
            NEXT_EXE_UNITS.Text = String.Empty
        Else
            NEXT_EXE_UNITS.Text = Row.Item("NEXT_EXE_UNITS")

        End If

        Dim CyclesRemaining As Integer

        CyclesRemaining = (Int32.Parse([NEXT_EXE_UNITS].Text) - Int32.Parse([READING].Text))

        DataGrid1.DataSource = cmdSelect.ExecuteReader

        DataGrid1.DataBind()



Kai-What?
 
Is there an error or some question here?
And why would you only display one value in a datagrid? Just display it in a label, literal or textbox.
 
Yeah, my bad, basically what I want is a column where it shows me the total of this:

Code:
CyclesRemaining = (Int32.Parse([NEXT_EXE_UNITS].Text) - Int32.Parse([READING].Text))

What I get in the datagrid is anywhere from 10-100 results and for each row, I would like the product of cyclesremaining showing, I can do it in text boxes, but that would only show me 1st row, unless I somehow loop through the results, but even then it would not be showing directly next to the other results in the datagrid.

Kai-What?
 
I tried the template column, but since my "new" field is a calculated field, it did not like it in the code, but maybe I am doing something wrong??

DataBinding: 'System.Data.Common.DbDataRecord' does not contain a property with the name 'CyclesRemaining'

Is the error I receive from this code:

Code:
<asp:TemplateColumn HeaderText="Cyckes Remaining">
						<ItemTemplate>
							<%# DataBinder.Eval(Container.DataItem, "CyclesRemaining") %>
						</ItemTemplate>
					</asp:TemplateColumn>



Kai-What?
 
Calculate what you need to caculate in your SQL statement
If you are just taking columnA and subtracting columnB then it is a very easy sql statemnt.

Select NEXT_EXE_UNITS, READING, NEXT_EXE_UNITS - READING As CyclesRemaining
From YourTable
Where ...etc etc...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top