Hi, how do I go about hiding a row from a gridview of an advertisement table where the advertisement_end_date is less than System.dateTime.Now? In other words: make the row invisible when the advertisement end date has passed. Below is the code behind for my advertisements...
Imports System
Imports System.Data.SqlClient
Imports System.Text.RegularExpressions
Imports System.Web.UI
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web.UI.WebControls
Public Class Vacancy
Inherits System.Web.UI.Page
Private advertisement_end_date As String
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("DataConnectionString").ConnectionString
Using con As New SqlConnection(constr)
con.Open()
Using cmd As New SqlCommand("SELECT department_name FROM Department", con)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
con.Close()
DeptGridView.DataSource = dt
DeptGridView.DataBind()
End Using
End Using
End Using
End Using
End Sub
Private Shared Function GetData(query As String) As DataTable
Dim strConnString As String = ConfigurationManager.ConnectionStrings("DataConnectionString").ConnectionString
Using con As New SqlConnection(strConnString)
Using cmd As New SqlCommand()
cmd.CommandText = query
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As New DataSet()
Dim dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Protected Sub DeptGridView_OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim department_name As String = DeptGridView.DataKeys(e.Row.RowIndex).Value.ToString()
Dim PositionGridView As GridView = TryCast(e.Row.FindControl("PositionGridView"), GridView)
PositionGridView.DataSource = GetData(String.Format("select advertisement_id, advertisement_name, advertisement_path, advertisement_end_date from Advertisement where advertisement_department='{0}'", department_name))
PositionGridView.DataBind()
End If
End Sub
Protected Sub PositionGridView(sender As Object, PositionGridView As GridViewRowEventArgs) Handles DeptGridView.RowDataBound
If (PositionGridView.Row.RowType = DataControlRowType.DataRow) Then
Dim constr As String = ConfigurationManager.ConnectionStrings("DataConnectionString").ConnectionString
Using con As New SqlConnection(constr)
con.Open()
Dim gv As GridView = CType(PositionGridView.Row.FindControl("PositionGridView"), GridView)
Using cmd As New SqlCommand("SELECT a.advertisement_id, a.advertisement_name, a.advertisement_end_date, a.advertisement_path FROM ((Advertisement AS a INNER JOIN Department AS d ON a.advertisement_department = d.department_name) INNER JOIN Department ON a.advertisement_id = d.advertisement_id);", con)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
con.Close()
gv.DataSource = dt
gv.DataBind()
End Using
End Using
End Using
End Using
End If
End Sub
End Class