Click here to Skip to main content
14,667,169 members
Rate this:
Please Sign up or sign in to vote.
See more:
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
Posted
Updated 5-Jan-12 9:08am
v2
Comments
darkDercane 5-Jan-12 10:07am
   
in the databound event you can do a simple calculation of the cells content :P you're seeking that? :P
Roancho Khan 5-Jan-12 10:25am
   
thank you fro the quick responce darkdercane.

I tryied just now with TemplateField below is the code


<asp:TemplateField HeaderText="Age">
<itemtemplate>
<%# 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
devbtl 5-Jan-12 11:36am
   
javascript is another alternate for your problem

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

Solution 2

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[^]
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 3

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>

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

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




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