在GridView中的计算 [英] calculation in the gridview

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

问题描述

我有什么方法可以在gridview中执行计算并显示它.
例如,我必须通过从数据库中获取出生日期来计算年龄".

从评论中添加:

我刚才用TemplateField试过了,下面是代码

Is there any way that i can perform the calculation in the gridview and diplay it.
For example i have to calculate the "Age " by fetching the date of birth from database.

Added from comment:

I tryied just now with TemplateField below is the code

<asp:TemplateField HeaderText="Age">
<%# Eval("CreatedOn", "{0:c0}")%>
<%# incrementsum(Eval("CreatedOn"))%>


下面是递增和函数:


and below is the incrementsum function :

<script   runat="server">
      int incrementsum(DateTime   CreatedOn)
      {
            DateTime dtage = new DateTime(CreatedOn);
            TimeSpan ts = DateTime.Now.Subtract(dtage);
            int age = ts.Days / 365;
            return (age);
      }
</script>


不起作用.

您能告诉我如何使用数据示例和示例
来处理数据绑定事件吗?
在此先谢谢您


is not working.

Can you please tell me how to work with databound event with and example

Thank you in advance

推荐答案

尝试在DataGridView的RowDataBound事件中编写代码

Try to write code at RowDataBound event of DataGridView

if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // add the Age to the running total variables
        Age += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, _
          "Age"));
  
    }



有关更多参考,请参见 [



For more reference have a look there[^]


感谢RaviRanjankr,我能够获取年龄值,但是现在我在对年龄进行排序时遇到问题.下面是gridview代码
Thank you RaviRanjankr, i was able to get age value, but now i am facing problem while sorting the age.below is the gridview code
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" EmptyDataText="Data Not Found !!!" Width="100%" DataSourceID="SqlDataSource1" PageSize="20" OnRowDataBound="GridView1_RowDataBound"  OnSorting="GridView1_Sorting" CssClass="gridviewheader">
       <FooterStyle BackColor="#383838" Font-Bold="True" ForeColor="White" Font-Names="Arial,Helvetica,sans-serif" Font-Size="9px"/>
       <RowStyle BackColor="#9f0000" ForeColor="white" Font-Names="Arial,Helvetica,sans-serif" Font-Size="9px" />
       <AlternatingRowStyle BackColor="#EFF3FB" ForeColor="#333333" />
       <Columns>


           <asp:BoundField DataField="SLNO" HeaderText="SL #" SortExpression="SLNO" ItemStyle-ForeColor="#9f0000" ItemStyle-BackColor="#f7d9b0">
           <ItemStyle HorizontalAlign="Center"></ItemStyle>
           </asp:BoundField>
           <asp:BoundField HeaderText="Age" SortExCan yopu

pression="Age">
           <ItemStyle HorizontalAlign="center"></ItemStyle>
           </asp:BoundField>


       </Columns>
       <PagerStyle BackColor="#e9b76b" ForeColor="#9f0000" HorizontalAlign="Center" />
       <SelectedRowStyle BackColor="#e9b76b" Font-Bold="True" ForeColor="#9f0000" />
       <HeaderStyle BackColor="#9f0000" Font-Bold="True" ForeColor="White" Font-Size="11px" Font-Names="Arial, Helvetcica, Sans-Serif" />
       <EditRowStyle BackColor="#e9b76b" />
       <AlternatingRowStyle BackColor="#e9b76b" ForeColor="#9f0000" />
       </asp:GridView>


-------------------------------------------------- ------------------------------
以下是排序代码
受保护的void GridView1_Sorting(对象发送者,GridViewSortEventArgs e)
{//获取GridViewRows

var rows = GridView1.Rows.Cast< GridViewRow>().Select(a => new
{
Number = Convert.ToInt32(GridView1.DataKeys [a.RowIndex] .Value),
NumberText =((Label)a.FindControl("Label1")).Text
});
//相应地获取排序方向
SortDirection sortDirection = ViewState ["SortOrder"] == null吗? SortDirection.Descending:
(SortDirection)Enum.Parse(typeof(SortDirection),ViewState ["SortOrder"].ToString())== SortDirection.Ascending?
SortDirection.Descending:SortDirection.Ascending;
ViewState ["SortOrder"] = sortDirection;

如果(sortDirection == SortDirection.Ascending)
rows = rows.OrderBy(a => a.NumberText);
其他
rows = rows.OrderByDescending(a => a.NumberText);

GridView1.DataSource = rows.ToList();
GridView1.DataBind();
}

您能帮我对值进行排序吗?

预先谢谢你


--------------------------------------------------------------------------------
below is the sorting code
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{ //Get GridViewRows

var rows = GridView1.Rows.Cast<GridViewRow>().Select(a => new
{
Number = Convert.ToInt32(GridView1.DataKeys[a.RowIndex].Value),
NumberText = ((Label)a.FindControl("Label1")).Text
});
//Get Sort Direction accordingly
SortDirection sortDirection = ViewState["SortOrder"] == null ? SortDirection.Descending :
(SortDirection)Enum.Parse(typeof(SortDirection), ViewState["SortOrder"].ToString()) == SortDirection.Ascending ?
SortDirection.Descending : SortDirection.Ascending;
ViewState["SortOrder"] = sortDirection;

if (sortDirection == SortDirection.Ascending)
rows = rows.OrderBy(a => a.NumberText);
else
rows = rows.OrderByDescending(a => a.NumberText);

GridView1.DataSource = rows.ToList();
GridView1.DataBind();
}

Can you please help me to sort the value

Thank you in advance


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

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