Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi 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
Comments
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?

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;
GridView1.DataBind();


Now in your Grdview the add one TemplateField and add Lable in it like
<templatefield>
   <itemtemplate>
       <asp:lable id="lblTotal" runat="Sever" text="<%# Bind("Total") %>" xmlns:asp="#unknown" />
   </itemtemplate>
</templatefield>
 
Share this answer
 
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
 
Share this answer
 
SqlCommand cmd = new SqlCommand("SELECT * FROM RNDTABLE",con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt=new DataTable();
        da.Fill(dt);

        dt.Columns.Add("GROSS_SAL", typeof(decimal), "(SAL +(SAL*.10))-100+5");

C#
GridView1.DataSource = dt;
       GridView1.DataBind();
 
Share this answer
 

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