DataGrid Calculation

How would you run a calculation on a dynamic datagrid column?
Example,  One of the databas columns has values like 15.34 = "Retardation" column  the equation is
Retardation*(D.Text/S.Text) = Column Calculated value. is it possible to calculate a value for each row while you are running a select all from Table statement.???




devlinsAsked:
Who is Participating?
 
PeteMcConnect With a Mentor Commented:
I think this is the sort of information you are after

this assumes you are getting the initial data into a dataset  I've called mine myDataSet

//Create a new colum
DataColumn Calculate = new DataColumn();
//Name the column
Calculate.ColumnName = "Calculated";
//set the type of data for the column
Calculate.DataType = System.Type.GetType("System.Int32");
//Set the default value of the column
Calculate.DefaultValue = 0;
//Add the new column into the Dataset
myDataSet.Tables[0].Columns.Add(Calculate);

//iterate through the rows in the Table
foreach(DataRow drow in myDataSet.Tables[0].Rows)
{
       //Calculate the value and store it for each row in the column created
       drow["Calculated"]=(int)(Double.Parse(drow["freight"].ToString()) *(Double.Parse(D.Text)/Double.Parse(S.Text)));

}
0
 
PeteMcCommented:
sorry  didn't change that  in yours "freight"  would be "Retardation"  I used freight in my test  also this assumes "D" and "S" are textboxes
0
 
rajaloysiousCommented:
If this is possible you can avoid all unnecessary code
I guess you can do that in the sql itself

select Retardation, Retardation*(col3/col4) as CALCULATED_VALUE... from <table_name>...

cheers
0
 
grayeCommented:
0
 
devlinsAuthor Commented:
I attached the datagrid code and the code behind to see if this is being done correctly..


The only problem is that it is not populating the Calculate column with the computed solution......            
or the defualt value............





<asp:datagrid id=DGrid runat="server" Width="896px" AllowSorting="True" PageSize="250" Height="431px" OnEditCommand="Edit_Click" OnCancelCommand="CancelIt" OnUpdateCommand="OnUpdate_Click" DataMember="Main_Waveplate_Inv" DataSource="<%# DataS %>" AutoGenerateColumns="False" BorderColor="#DEBA84" BorderStyle="None" CellSpacing="2" BorderWidth="1px" BackColor="#DEBA84" CellPadding="3" DataKeyField="Part_Key">
                                                      <FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
                                                      <SelectedItemStyle Font-Bold="True" ForeColor="Silver" BackColor="#738A9C"></SelectedItemStyle>
                                                      <EditItemStyle Font-Bold="True" BackColor="Gray"></EditItemStyle>
                                                      <AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
                                                      <ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
                                                      <HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#5283B5"></HeaderStyle>
                                                      <Columns>
                                                            <asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" EditText="Reserve"></asp:EditCommandColumn>
                                                            <asp:BoundColumn DataField="Inventory_Status" SortExpression="Inventory_Status" HeaderText="Inventory_Status"></asp:BoundColumn>
                                                            <asp:BoundColumn DataField="Part_Key" SortExpression="Part_Key" ReadOnly="True" HeaderText="Part_Key"></asp:BoundColumn>
                                                            <asp:BoundColumn DataField="Diameter" SortExpression="Diameter" ReadOnly="True" HeaderText="Diameter"></asp:BoundColumn>
                                                            <asp:BoundColumn DataField="Retardation" SortExpression="Retardation" ReadOnly="True" HeaderText="Retardation"></asp:BoundColumn>
                                                            <asp:BoundColumn DataField="Retardation_Dec" SortExpression="Part_Key" ReadOnly="True" HeaderText="Retardation_Dec"></asp:BoundColumn>
                                                            <asp:BoundColumn DataField="Reserve_Customer" SortExpression="Reserve_Customer" HeaderText="Reserve_Customer"></asp:BoundColumn>
                                                            <asp:TemplateColumn SortExpression="Reserve_User" HeaderText="Reserve_User">
                                                                  <ItemTemplate>
                                                                        <asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Reserve_User") %>' ID="Reserve_User"/>
                                                                  </ItemTemplate>
                                                                  <EditItemTemplate>
                                                                        <asp:DropDownList runat="server" SelectedIndex='<%#GetSalesIndex(DataBinder.Eval(Container.DataItem, "Reserve_User").ToString()) %>' id="R_User">
                                                                              <asp:ListItem>ROBERTD</asp:ListItem>
                                                                              <asp:ListItem>EMA</asp:ListItem>
                                                                              <asp:ListItem>PHILD</asp:ListItem>
                                                                              <asp:ListItem>SYSADM</asp:ListItem>
                                                                        </asp:DropDownList>
                                                                  </EditItemTemplate>
                                                            </asp:TemplateColumn>
                                                            <asp:TemplateColumn SortExpression="Calculate" HeaderText="Calculate"></asp:TemplateColumn>
                                                      </Columns>
                                                      <PagerStyle NextPageText="NEXT" BorderColor="Gold" PrevPageText="PREVIOUS" HorizontalAlign="Right"
                                                            ForeColor="#5283B5" Position="TopAndBottom" BackColor="CornflowerBlue"></PagerStyle>
                                                </asp:datagrid></DIV

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Conn.Open();
                        
                        string sqlC = "(select top 200 * from Main_Waveplate_Inv where  ((Retardation between " + E0.Text + " and " + E.Text + ") or (Retardation between " + E.Text + " and " + E0.Text + ")) and Diameter = '" + D.SelectedItem.Text + "') order by Retardation desc" ;
                        Eq1.Text + " and Diameter = '" + Diameter.Text + "')";
                        
            
      


                        Waveplate.Retardation/(Tx02.Text *DL.Text)";

                        DataA = new System.Data.SqlClient.SqlDataAdapter(sqlC, Conn);
                        DataTable DTable = new DataTable();
                  
                        Retardation)(@Eq1,@Eq2)",Conn) ;
                  
                        double d3 = double.Parse(DL.Text);
                  
                        DataRow IRow;
                        
                        IRow = DTable.NewRow();
                  

DataColumn Calculate = new DataColumn();
                        
                        Calculate.DefaultValue=0;
DataS.Tables[0].Columns.Add(Calculate);

int i1 = (int)(d1*d2/d3);

                        foreach(DataRow dRow in DataS.Tables[0].Rows)
                        {
                        dRow["Calculted"]=(int)( double.Parse(IRow["Retardation"].ToString()) ) * ( double.Parse(Tx02.Text) / double.Parse(DL.Text) );
                        
                        
                        
                        }
      
//Calculate = i1.ToString();
                        DataA.Fill(DataS, "Main_Waveplate_Inv");
                        DGrid.DataSource = DataS;
                        DGrid.DataMember = "Main_Waveplate_Inv";
                  
                  
                        
                        

                        
                                    
                  

                        
                              DataS.Tables.Add();
                              DGrid.DataBind();
                              Conn.Close();






0
All Courses

From novice to tech pro — start learning today.