Click here to Skip to main content
13,045,285 members (55,497 online)
Rate this:
Please Sign up or sign in to vote.
Lets say that I have a table with three columns. Name, Salary, Bonus.
Now I want to create a gridView to represent the data in the table with a fourth column contains the bonus calculation ( salary * bonus).
In other words, I will have a three columns table, and a four column gridView.

I Hope that any one can help me.
Many Thanks in advance.
Posted 24-Feb-13 3:39am
Shanu2rick 24-Feb-13 9:52am
The best way to perform this task is by using Item Template in gridView designed by you. All you need to do is create four columns. Assign the values of the three column directly and the fourth column's programatically. I hope this help will sort you out with your help.
Sandeep Mewara 24-Feb-13 11:17am
Ok, so where are you stuck? What have you tried so far?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

try the following solution...
Once your retrieve data from database to DataTable the add one new column in the same data table like
Assume your datatable name is dt then write,
 dt.Columns.Add(new DataColumn("Total", typeof(decimal))); 
 foreach(DataRow dr in dt.Rows)
   dr["Total"] = Convert.ToDecimal(dr["Salary"]) * Convert.ToDecimal(dr["Bonus"])
 Gridview1.DataSource = dt;

Now in your Grdview the add one TemplateField and add Lable in it like
       <asp:lable id="lblTotal" runat="Sever" text="<%# Bind("Total") %>" xmlns:asp="#unknown" />
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

first of all thank you for your interesting,
regarding the second comment by Sandeep Mewara, I read the data and made my calculation in a dataReader, but I don't know how to assign the value to a new column and and make the new column appear in the gridView.

what I am really trying to do is the following.
In the table I have one column holds the amount value which can be number of experience years, or it can be a percentage of bonus, or percentage of inflation.
in the second column I have the allowance field.
what I am trying to do is to read the second column and find the allowance type, then depending on the type of that allowance I can make some calculations for example:
if the allowance type is experience then I will multiply the allowance Amount which is number of years by a certain amount of money from another table depending on the job. and if the allownace type is bonus I have to multiply the amount by the salary in another table using C# code.

Thank you
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

SqlCommand cmd = new SqlCommand("SELECT * FROM RNDTABLE",con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt=new DataTable();
        dt.Columns.Add("GROSS_SAL", typeof(decimal), "(SAL +(SAL*.10))-100+5");

GridView1.DataSource = dt;

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100