Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
binddatamsggrid()
End If
end sub
Private Sub binddatamsggrid()
Dim conn As String = connectionstring.connectionforumdt
Dim msgconn As New SqlConnection
Dim unrdval As Integer = 0
msgconn.ConnectionString = connectionstring.connectionrough
msgconn.Open()
Dim noticmd As New SqlCommand("SELECT [msgid],[unrd],[touser],[dt] [fromuser],(select [fname] from junk where [ID] = [fromuser]) AS fromuserfname, [touser], SUBSTRING([msg],1,25) AS msg,(select [imageid] from junk where ID=[fromuser]) AS imageid FROM [msg]
WHERE (([touser] = @touser) AND ([unrd] = @unrd)) ORDER BY [msgid] DESC ", msgconn)
Dim notida As New SqlDataAdapter(noticmd)
noticmd.Parameters.AddWithValue("@touser", Page.User.Identity.Name)
noticmd.Parameters.AddWithValue("@unrd", unrdval)
'da = New SqlDataAdapter
Dim ds As New DataSet()
notida.Fill(ds)
msggridview.DataSource = ds
msggridview.DataBind()
End Sub
Protected Sub msggrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles msggrid.RowDataBound
' searching through the rows
If e.Row.RowType = DataControlRowType.DataRow Then
Dim isnew As Integer = (DataBinder.Eval(e.Row.DataItem, "unrd"))
If isnew = "0" Then
e.Row.BackColor = Color.FromName("#D3D3D3")
e.Row.ForeColor = Color.FromName("#FFFFFF")
Dim currentdate As Label = TryCast(e.Row.FindControl("dt"), Label)
currentdate.Text = DateTime.Now
'==== datetime =======
Dim oldate As Label = TryCast(e.Row.FindControl("datetime"), Label)
Dim olddate1 As DateTime = Convert.ToDateTime(oldate.Text)
Dim newdate As DateTime = Date.Now
Dim dif As Double = DateDiff(DateInterval.Second, olddate1, newdate)
If dif < 60 Then
calc = "Just now"
ElseIf dif > 60 And dif < 3600 Then
calc = dif / 60 & " " & "minutes ago"
ElseIf dif < 86400 And dif > 3600 Then
calc = dif / 3600 & " " & "Hours ago "
ElseIf dif < 172800 And dif > 86400 Then
calc = "Yesterday"
ElseIf dif > 172800 Then
calc = olddate1
End If
End If
End If
End Sub
NOw how can i bind gridview with calculated calc variable to some lable in gridview , thanks
sATsvelKe
Member
120 Points
179 Posts
how to Bind data to label in gridview according to if else conditions ???
Aug 18, 2012 03:11 PM|LINK
here is my code ..it is for example
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
binddatamsggrid()
End If
end sub
Private Sub binddatamsggrid()
Dim conn As String = connectionstring.connectionforumdt
Dim msgconn As New SqlConnection
Dim unrdval As Integer = 0
msgconn.ConnectionString = connectionstring.connectionrough
msgconn.Open()
Dim noticmd As New SqlCommand("SELECT [msgid],[unrd],[touser],[dt] [fromuser],(select [fname] from junk where [ID] = [fromuser]) AS fromuserfname, [touser], SUBSTRING([msg],1,25) AS msg,(select [imageid] from junk where ID=[fromuser]) AS imageid FROM [msg] WHERE (([touser] = @touser) AND ([unrd] = @unrd)) ORDER BY [msgid] DESC ", msgconn)
Dim notida As New SqlDataAdapter(noticmd)
noticmd.Parameters.AddWithValue("@touser", Page.User.Identity.Name)
noticmd.Parameters.AddWithValue("@unrd", unrdval)
'da = New SqlDataAdapter
Dim ds As New DataSet()
notida.Fill(ds)
msggridview.DataSource = ds
msggridview.DataBind()
End Sub
Protected Sub msggrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles msggrid.RowDataBound
' searching through the rows
If e.Row.RowType = DataControlRowType.DataRow Then
Dim isnew As Integer = (DataBinder.Eval(e.Row.DataItem, "unrd"))
If isnew = "0" Then
e.Row.BackColor = Color.FromName("#D3D3D3")
e.Row.ForeColor = Color.FromName("#FFFFFF")
Dim currentdate As Label = TryCast(e.Row.FindControl("dt"), Label)
currentdate.Text = DateTime.Now
'==== datetime =======
Dim oldate As Label = TryCast(e.Row.FindControl("datetime"), Label)
Dim olddate1 As DateTime = Convert.ToDateTime(oldate.Text)
Dim newdate As DateTime = Date.Now
Dim dif As Double = DateDiff(DateInterval.Second, olddate1, newdate)
If dif < 60 Then
calc = "Just now"
ElseIf dif > 60 And dif < 3600 Then
calc = dif / 60 & " " & "minutes ago"
ElseIf dif < 86400 And dif > 3600 Then
calc = dif / 3600 & " " & "Hours ago "
ElseIf dif < 172800 And dif > 86400 Then
calc = "Yesterday"
ElseIf dif > 172800 Then
calc = olddate1
End If
End If
End If
End Sub
NOw how can i bind gridview with calculated calc variable to some lable in gridview , thanks
oned_gk
All-Star
31017 Points
6352 Posts
Re: how to Bind data to label in gridview according to if else conditions ???
Aug 18, 2012 03:25 PM|LINK
Dim lblCalc As Label = CTYPE(e.Row.FindControl("LabelCalc"), Label) LblCalc.text = calc<asp:TemplateField> <ItemTemplate> <asp:Label ID="LabelCalc" runat="server" Text=""></asp:Label> </ItemTemplate> </asp:TemplateField>sATsvelKe
Member
120 Points
179 Posts
Re: how to Bind data to label in gridview according to if else conditions ???
Aug 18, 2012 08:04 PM|LINK
I have checked this but its not working ? is there any other way pls post it . thanks
sATsvelKe
Member
120 Points
179 Posts
Re: how to Bind data to label in gridview according to if else conditions ???
Aug 18, 2012 08:10 PM|LINK
hey this is working . thanks for your help