Hi smile,
In GridView OnRowDataBound Event we are subtracting DateOfBirth column value with current Date. On That basic we are calculating the 91 days in the if condition. if total days is greater than 91 the update button is disable.
Please refer below Code.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ForeColor="#333333"
RowStyle-Wrap="false" HeaderStyle-Wrap="false"
Class="table table-striped table-bordered table-hover" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField>
<%--<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" />
</ItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="Calf ID">
<ItemTemplate>
<asp:Label ID="lbl_No" runat="server" Text='<%# Eval("CalfID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dam ID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("DamID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDam" runat="server" Text='<%# Eval("DamID") %>' class="form-control"
Width="150"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sire Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("SireName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSire" runat="server" Text='<%# Eval("SireName") %>' class="form-control"
Width="150"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date of Birth">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("DateofBirth") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDate" runat="server" Text='<%# Eval("DateofBirth") %>' class="form-control"
Width="150"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Calf Tag">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("CalfTag") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCalfTag" runat="server" Text='<%# Eval("CalfTag") %>' class="form-control"
Width="150"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("Gender") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtGender" runat="server" Text='<%# Eval("Gender") %>' class="form-control"
Width="150"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Weight">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("Weight") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtWeight" runat="server" Text='<%# Eval("Weight") %>' class="form-control"
Width="150"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pen Name ">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("PenName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Next Date">
<ItemTemplate>
<asp:Label ID="Label70" runat="server" Text='<%# Eval("NextDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="btnCancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#CCCCCC" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "select CalfID,DamID,SireName,DateofBirth,CalfTag,Gender,Weight,PenName,NextDate,DateofBirth,CONVERT(VARCHAR, DATEADD(MONTH, 3, CONVERT(VARCHAR, DateofBirth, 103)), 103) as [Date] from tblCalf order by CalfID desc";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime dob = Convert.ToDateTime((e.Row.FindControl("Label3") as Label).Text);
DateTime currentDate = DateTime.Now;
int days = currentDate.Subtract(dob).Days;
if (days >= 91)
{
((LinkButton)e.Row.FindControl("btnEdit")).Enabled = false;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "select CalfID,DamID,SireName,DateofBirth,CalfTag,Gender,Weight,PenName,NextDate,DateofBirth,CONVERT(VARCHAR, DATEADD(MONTH, 3, CONVERT(VARCHAR, DateofBirth, 103)), 103) as [Date] from tblCalf order by CalfID desc"
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand(query, con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Using
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dob As DateTime = Convert.ToDateTime((TryCast(e.Row.FindControl("Label3"), Label)).Text)
Dim currentDate As DateTime = DateTime.Now
Dim days As Integer = currentDate.Subtract(dob).Days
If days >= 91 Then
CType(e.Row.FindControl("btnEdit"), LinkButton).Enabled = False
End If
End If
End Sub
Screenshot