你可以在网格视图上进行计算吗? [英] Can you do a calculation on a Grid View?

查看:72
本文介绍了你可以在网格视图上进行计算吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView,它绑定了数据。我有三列。两个有数据但第三个没有数据。我正在尝试进行计算,如果列B为1或2,则计算A列,并将答案放在C列。



示例:

<前lang =HTML> A栏B列C
500 168 03
500 120 04





有没有办法这样做?

解决方案

你可以这样做,但你需要确保你有一个虚拟列定义你的SQL将添加你想要计算结果的列名:



 选择 *, null  '  C列' 来自 .... 





这将允许您的数据源绑定,因为列将存在以存储结果。



然后执行RowD GridView上的ataBound事件应该是这样的:



  protected   void  GridView1_RowDataBound( object  sender,GridViewRowEventArgs e)
{
DataRow rowView =(DataRow)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
// 执行计算;
int value = Convert.ToInt32(rowView [ A列])* Convert.ToInt32(rowView [ B列]);
e.Row.Cells [e.Row.Cells.Count - 1 ]。Text = value 的ToString();
}
}


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:

Column A      Column B     Column C
500            168            03
500            120            04



Is there a way of doing it that way?

解决方案

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:

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:

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


这篇关于你可以在网格视图上进行计算吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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