Click here to Skip to main content
16,012,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I am trying to add a column which displays Yes Or No Text based on column values if the previous 2 columns(Date Columns) difference is more than 1 month to Starting Date.

The Previous 2 columns are Start Date and End Date and the values of these 2 columns are coming from database. Please let me know if you need more information

Here is the code I am adding..

C#
Protected Sub GridView1_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim dtDOJ As DateTime = Convert.ToDateTime(e.Row.Cells(4).Text)
        Dim dtDOJ1 As DateTime = Convert.ToDateTime(e.Row.Cells(5).Text)
        
        If dtDOJ1 >= dtDOJ.AddMonths(1) Then
            e.Row.Cells(6).Text = "Yes";
        Else
            e.Row.Cells(6).Text = "No";
        End If
    End If


End Sub
Posted
Updated 5-Jun-13 20:59pm
v7
Comments
Abhinav S 6-Jun-13 2:55am    
Are you facing some issues with this code? Is it not working?
Thanks7872 6-Jun-13 2:58am    
You have not mentioned what the issue is. Well,i think you faced issue in finding the values of cells right? And where have you taken this yes/no values?dropdown,textbox,label?
Member 9861478 6-Jun-13 3:03am    
Oops! Thank you for probing. I haven't added any textbox or label. Really new to use grid and I am in a migration project..Could you please guide me on this?
Member 9861478 6-Jun-13 3:10am    
I searched in google and found to add Label..May that helps for next steps sorry abt that...
Member 9861478 6-Jun-13 2:58am    
Yes Abhinav. The code is not working..seems no error but no result.

Have a look at below code(Though its in C#,use converter for VB):

C#
<asp:gridview id="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound" xmlns:asp="#unknown">
    <columns>
        <asp:templatefield>
            <itemtemplate>
               <asp:label id="lbl" runat="server" text=""></asp:label>
            </itemtemplate>
        </asp:templatefield>
    </columns>
</asp:gridview>


The event you used is ok.

C#
protected void lr_grid_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
                //Do your calculations and then assign text to lbl(that is label in templatefield)
           }
       }


For date calculations use DateTime.Compare Method[^]

Happy coding.. :)
 
Share this answer
 
v3
Comments
Member 9861478 6-Jun-13 3:14am    
Thank you . I will try and let you know....Really appreciated.
Member 9861478 6-Jun-13 3:47am    
Surprisingly, The label id is not getting inherited in code behind..any idea?
Thanks7872 6-Jun-13 3:49am    
It will not because its a template field.Access it as below


Label lbl_temp = (Label)e.Row.FindControl("lbl");
lbl.Text = your values;
Thanks7872 6-Jun-13 3:50am    
This code will create one label and then the value will be assigned to that label which ultimately shown in gridview.
Member 9861478 6-Jun-13 3:53am    
Thanks. Sorry for my ignorance..I am trying that..Really helpful..
Use [MSDN] DateTime.Compare Method[^].
Quote:
Compares two instances of DateTime and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance.

Parameters
t1
Type: System.DateTime
The first object to compare.
t2
Type: System.DateTime
The second object to compare.

Return Value
Type: System.Int32
A signed number indicating the relative values of t1 and t2.
Value Type            Condition
Less than zero        t1 is earlier than t2.
Zero                  t1 is the same as t2.
Greater than zero     t1 is later than t2.

 
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