将计算值表单表分配给GridView [英] assigning a calculated value form table to GridView

查看:49
本文介绍了将计算值表单表分配给GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可以说我有一个有三列的表。姓名,薪水,奖金。

现在我想创建一个gridView来表示表中的数据,第四列包含奖金计算(工资*奖金)。

换句话说,我将有一个三列表和一个四列gridView。



我希望任何人都可以帮助我。

非常感谢提前。

解决方案

尝试以下解决方案...

一旦从数据库检索数据到DataTable,就添加一个新的相同数据表中的列如

假设你的数据表名称是dt然后写,

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





现在在你的Grdview中添加一个TemplateField并添加Lable就像

<模板列> 
< itemtemplate>
< asp:lable id =lblTotalrunat =Severtext =<%#Bind(Total)%> xmlns:asp =#unknown/>
< / itemtemplate>
< / templatefield>


首先感谢你有趣的,

关于Sandeep Mewara的第二条评论,我读了数据并在dataReader中进行计算,但我不知道如何将值赋给新列并使新列出现在gridView中。



我真正想做的是以下几点。

在表格中,我有一列保存金额值,可以是经验年数,也可以是奖金的百分比,或者通货膨胀的百分比。

在第二栏我有津贴字段。

我想要做的是阅读第二列并找到津贴类型,然后依赖关于这种津贴的类型,我可以进行一些计算,例如:

如果津贴类型是经验,那么我将把津贴金额乘以另一张表中的一定数额的金额。在工作中。如果allownace类型是奖金,我必须使用C#代码将金额乘以另一个表中的工资。



谢谢


< blockquote>

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



 GridView1.DataSource = dt; 
GridView1.DataBind();


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.

解决方案

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>


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


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


GridView1.DataSource = dt;
       GridView1.DataBind();


这篇关于将计算值表单表分配给GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆