DataGrid Calculation

Posted on 2005-04-07
Last Modified: 2006-11-18
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.???

Question by:devlins
    LVL 2

    Accepted Solution

    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

    //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)));

    LVL 2

    Expert Comment

    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
    LVL 8

    Expert Comment

    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>...

    LVL 41

    Expert Comment


    Author Comment

    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>
                                                                <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">
                                                                            <asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Reserve_User") %>' ID="Reserve_User"/>
                                                                            <asp:DropDownList runat="server" SelectedIndex='<%#GetSalesIndex(DataBinder.Eval(Container.DataItem, "Reserve_User").ToString()) %>' id="R_User">
                                                                <asp:TemplateColumn SortExpression="Calculate" HeaderText="Calculate"></asp:TemplateColumn>
                                                          <PagerStyle NextPageText="NEXT" BorderColor="Gold" PrevPageText="PREVIOUS" HorizontalAlign="Right"
                                                                ForeColor="#5283B5" Position="TopAndBottom" BackColor="CornflowerBlue"></PagerStyle>


                            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();

    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";




    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    return array from dll 13 21
    c# string 6 28
    What does this  TN Map<T, TN>(T src) mean? 5 26
    Need help with a query 6 16
    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now