Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a GridView that has bound data on it. I have three columns. Two has data but the third has no data. I am trying to do a calculation that will calculate column A if column B is 1 or 2 and put the answer in column C.

Example:
HTML
Column A      Column B     Column C
500            168            03
500            120            04


Is there a way of doing it that way?
Posted
Updated 2-Feb-15 9:44am
v2
Comments
ZurdoDev 2-Feb-15 15:50pm    
I'd suggest doing it in Sql.
Computer Wiz99 2-Feb-15 16:05pm    
Okay, Thanks.
TheKarateKid 2-Feb-15 16:44pm    
As RyanDev suggested we do it on SQL server side in Stored Procedures, if there are tens of thousands of records. But I might do it DTOs or Entities by adding calculated readonly properties or GridView's RowDataBound. For example if you I want to display running total in the footer of each gridview page.

1 solution

You could do it, but you will need to ensure that you have a dummy column defined in your SQL that will add the column name you want to put your calculation result in:

SQL
select *, null 'Column C' from ....


This will allow your datasource to bind because the columns will exist to store the result in.

You then perform a RowDataBound event on the GridView that should be something like this:

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    DataRow rowView = (DataRow)e.Row.DataItem;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // perform your calculation;
        int value = Convert.ToInt32(rowView["Column A"]) * Convert.ToInt32(rowView["Column B"]);
        e.Row.Cells[e.Row.Cells.Count - 1].Text = value.ToString();
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900