Multiply two columns and store the result into third column

Hi,

 I am new to asp.net world. I am having 1 problem in a gridview.  

There are 3 template fields in my gridview namely Length , Width and Area. Length n Width contain textboxes and Area is having label. Problem is as soon as you enter the values in textboxes, the area should be calculated and reflected immediately in the area field of that row. I am using RowDataBound event gridview. But area is not getting update dynamically after changing the textfields.

 Code i m using is:

protected void gvArea_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int length = Convert.ToInt32(((TextBox)e.Row.FindControl("txtLength")).Text);
            int width = Convert.ToInt32(((TextBox)e.Row.FindControl("txtWidth")).Text);           

            ((Label)e.Row.FindControl("lblArea")).Text = Convert.ToString(length * width);
        }
    }

5
GagaDCoder
10/30/2008 5:12:32 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

2 Replies
5730 Views

Similar Articles

[PageSpeed] 7

Hi GagaDCoder ,

GagaDCoder:
Problem is as soon as you enter the values in textboxes, the area should be calculated and reflected immediately in the area field of that row.

Try to use javascript code to do this.

something like this :

  <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
        onrowdatabound="GridView2_RowDataBound">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox ID="txta" runat="server" Text='<%# Eval("id") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox ID="txtb" runat="server" Text='<%# Eval("count") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox ID="txtc" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <script>
    function dowork(a,b,res)
    {
      document.getElementById(res).value = parseInt( document.getElementById(a).value,10) +parseInt( document.getElementById(b).value,10) ;
    }
    </script>
  
        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox boxa = e.Row.FindControl("txta") as TextBox;
                TextBox boxb = e.Row.FindControl("txtb") as TextBox;
                TextBox boxc = e.Row.FindControl("txtc") as TextBox;

                boxb.Attributes.Add("onblur","dowork('"+boxa.ClientID+"','"+boxb.ClientID+"','"+boxc.ClientID+"')");
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable table = new DataTable();
                table.Columns.Add("id");
                table.Columns.Add("count");
                DataRow dr = table.NewRow();
                dr["id"] = 1;
                dr["count"] = 2;
                table.Rows.Add(dr);
                this.GridView1.DataSource = table;
                GridView1.DataBind();


                //------
                this.GridView2.DataSource = table;
                GridView2.DataBind();
            }
        }
 

Samu Zhang
Microsoft Online Community Support

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
0
Samu
11/3/2008 4:56:52 AM

Hi,

Instead of ((Label)e.Row.FindControl("lblArea")).Text = Convert.ToString(length * width); use the following code

 protected void gvArea_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int length = Convert.ToInt32(((TextBox)e.Row.FindControl("txtLength")).Text);
            int width = Convert.ToInt32(((TextBox)e.Row.FindControl("txtWidth")).Text);           

            e.Row.Cells[2].Text =Convert.ToString(length * width);
        }
    }


Thanks,
Kalpana Patel

Remember to click “Mark as Answer” on the post, if it helps you.
0
kyuti
11/3/2008 6:27:07 AM
Reply:

Similar Artilces:

How to subtract two numeric columns(bound) of a Datagrid and display the result in the third column?
Hi Everyone,I  have two numeric columns(databound-coming from SQL database) in my datagrid, 'PromoPrice' & 'CostPrice'. I need to calculate 'PromoPrice- CostPrice' and display the result in the third datagrid column. i.e.: column2-column1 = column3Any help anyone...Many Thanks in advance,Nico. In your query do this:Select .... , (PromoPrice - CostPrice) AS Answer, .....FROM yourTableKeep in mind you can do math functions in queries.DarmarkMark as Answer, if this reply answers your post. Hi Darmark, thanks for your fast answer.The fact is I'm using a single "select function"...

How to subtract two date columns(bound) of a Datagrid and display the result in the third column?
Hi Everyone,I  have two date columns(databound-coming from SQL database) in my datagrid, 'start date' & 'end date'. I need to calculate number of days with 'end date- start date' and display the result in the third datagrid column. i.e.: column2-column1 = column3Any help anyone?...ASAP please got a deadline to meet :) (SOS!!!)Many Thanks in advance,Arty.Aartee...we live more than once! Hi, there are several ways of doing it. One of them is creating a label in the TemplateColumn, use the Helper function to calculate the number of days.<ItemTemplate>   <...

Addition of two Columns in third Column.
Hi Guys, I got stucked in my project and i need your help. I have a data grid with data from sql 2005. I have got two amount columns - Amount1 & Amount2 with values from database. I want the addition of both in Total column (type: template) which I added in data grid view I also want the grand total of Total column in the bottom. Pls help  >I have got two amount columns - Amount1 & Amount2 with values from databaseWhy not change the select? ChangeSELECT  Amount1, Amount2 FROM FREDto SELECT  Amount1, Amount2, (Amount1 + Amount2) AS Amount3 FROM FRED...

Compare Two columns and update the Third Column
 Hi,I have three columns and want to update the third column based on the comparison of first two columns in the same table.For instance:Col1 - val1, val2, val3, val4, val5......Col2 - NULL, NULL, val1, NULL, val2....Col3 - TRUE, TRUE, FALSE, FALSE, FALSE....Thanks for any help. When you define a column, you can define it as a "computed column". Make col3 a computed column and fill in the comparison expression.    Dear David,Thanks for your reply. Actually I have table with more then 7000 records in it. and all the columns are already defined. I was lookin...

how do i get the column i want if result has two columns with the same name?
 let's say my select command returns two columns with the same name "Id".(those two columns are from different tables) so would it be possible to get the data from the second column if i don't use the index? thanks in advance   Try to use alias: SELECT table1.id as id1, table2.id as id2, othercolumn from......Limno...

display the sum of two datagrid columns into third column
hi... there..     i have a datagrid with columns named Price ,Quantity and total. now in the total column i  wanna display the prices which comes by multiplyiing the price and quantity (like  total=Price * quantity). I'm workin in asp.net +vb.net.                     may i knw hw can i do this.. plz write the code.. thanks.         Hi, do like this..suppose u have,        string ProductPrice = ...

When is a column not a column?
I have several extremely similar datawindows that I have to maintain that go against the same table. Recently, I had to add a column to that table, and also add the column to ALL of these datawindows that go against this table. I added the column to the first datawindow, set up the edit control style, etc. I then did a copy/paste of the columns edit control to the remaining dw's (of course remembering to first add the column in SQL). Everything (update props, column specs, column props) seems to indicate that the pasted column is linked to the column I selected, HOWEVER, when I...

how to split the content of one column in the objectdatasource into two parts and let them display in two columns in the GridView?
i combine the title and the body of one article and store it in one column in the database,but i want to show the title and body seperately in the GridView,how should i achieve this goal?thank you so much for your advice!!! you can write the logic of splitting the column into two (title and body) in the query itself by which you are filling the datasource..Thanx, [KaushaL] || BloG || Profile || Microsoft MVP"I would love to change the world, but they won’t give me the source code"Don't forget to click "Mark as Answer" on the post that helped you.This credits that me...

Grouping two columns into one column with GridView
I would like to modify the default output of a gridview to group two columns into one column. This is probably best explained by example. So, lets say there is a simple gridview with the output something like this:Name           Description                           Date------------------------------------------------------------------Foo            Some description of foo     &nb...

Combine Two Different Columns into one column
Hi Guys,   I have twotables(Employee and Borrower). In Employee table have EMPID and Borrower table have BorrowerID. I want to comebine these two columns into one column as EMPID  in Employee table. Can  any one help? Thanks what is realtion between to tables ?   Use    Union Thanks for reply. Acutally Employee table EMPID, NAME Columns. in this Employee table had 12000 rows. Borrower table has BorrowerID,BType etc.. .Here Employee ID and BorrowerID both are same. This table has 16000 rows. Now my what i want to do is Come EMPID and BorrowerID and ...

3 column design insetad of two column
I would like to have 3 column design for csk, similar to www.buysellcommunity.com. How can I achieve this? What changes do I need to make to the CSS? Does anybody has sample code?Rafal Zikbay.com - Free Classifieds (Search Over 5 mln jobs on Zikbay.com) DNN Direct I don't know what you are using currently, but this article describes how to design a website with Pure CSS (and the last example is a 3 colum design): http://www.singingeels.com/Articles/How_To_Pure_CSS_Design.aspx-Timothy Khourihttp://www.SingingEels.com/Developer / Architect / Author There are some really good CSS templ...

Is it possible to limit the items in a dataList control to two columns and two rows?
or with sql?   to have less columns select less field to have less row use the top function. I really suggest you do not use * to get all fields its longer and normally you do not need all tables fieldDo not forget to mark as answer all message who help you.Don't forget to check if you can answer some of our question to. sorted thanks...

two db table column, one datagrid column
Dear all, I have a db table which have two column, first name and last name. Can I show them in one datagrid column? Thanks Yes you may. Through the usage of the TemplateColumn, yes. An example of this: <code> <asp:DataGrid runat="server" ID="dgSomeGrid">     <Columns>         <asp:TemplateColumn>             <ItemTemplate>                 <%# DataBinder.Eval(Container.DataSource, "LastName") %&g...

two column repeater control?
I want to display my data in a two column repeater control.  Art some point it may go to three columns.  My control looks like this:          <asp:Repeater ID="Repeater1" runat="server">            <HeaderTemplate>              <table class="p_innerTable">            </HeaderTemplate>     ...

Web resources about - Multiply two columns and store the result into third column - asp.net.presentation-controls

Multiply Records - Wikipedia, the free encyclopedia
... secondary sources about the topic. If notability cannot be established, the article is likely to be merged , redirected , or deleted . Multiply ...

Math Tapper Free: Multiply and Find on the App Store on iTunes
Get Math Tapper Free: Multiply and Find on the App Store. See screenshots and ratings, and read customer reviews.

Singaporeans spurn push to go forth and multiply
Singaporeans spurn push to go forth and multiply

It's complicated - customer interaction channels multiply
CSO Australia - News, Industry Blogs, Tools and Resources for Data Security Executives

Nazi bride school: go forth and multiply its creed
Unearthed documents shed light on this mysterious and sinister facet of the Third Reich.

Volkswagen says 11 million cars hit by emissions scandal, probes multiply
Volkswagen AG said a scandal over falsified US vehicle emission tests could affect 11 million of its cars worldwide as investigations of its ...

How do japanese multiply?? - YouTube
Well a rather interesting video ! How japanese multiply, a rather simple mathematic computation,or not? :D Please enjoy :D!

As cafes multiply, so do 'coffee cats'
On May 12, American ice cream brand Haagen Dazs refurbished one of its key outlets in Shanghai's Nanjing road and began switching up its products. ...

Smartphone apps multiply, but so do germs
Smartphone apps multiply, but so do germs

Top 10 Twitter Trends of the Week: 'Little Monsters' Multiply and Pop Grips the Chart
Pop stars command the five largest followings on Twitter, so a pop-heavy list of trends should be no surprise this week or any week.

Resources last updated: 1/15/2016 1:58:15 AM