when i try this code to display in DataTable Row, it's displaying this text inside textbox <a href=Update.aspx?id=81>" & "My Link Text" & "</a>
Row("Date") = String.Format("<a href=Update.aspx?id=81>" & "My Link Text" & "</a>")
Then i tried this code using TemplateFiled,
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports system.IO
Public Class TemplateHandler
Implements ITemplate
Private columnID, columnText As String
Public Sub New(ByVal colText As String)
columnText = colText
End Sub
Private Sub ITemplate_InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
Dim lbl As New Label()
lbl.Text = columnText
container.Controls.Add(lbl)
End Sub
End Class
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Class Issues
Inherits System.Web.UI.Page
Dim aryDate, aryName, aryDate_Desc, aryDesc_Color_Date As New ArrayList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Table_MAINPAGE As DataTable
Dim Row As DataRow
Dim dcol As DataColumn
Dim myConnection As SqlConnection
Dim MySQL As String
Dim myCommand As SqlCommand
Dim myreader As SqlDataReader
Dim pFirst As Boolean = True
Dim Name_NotExist, Group_NotExist As Boolean
aryDate.Clear()
aryName.Clear()
aryDate_Desc.Clear()
aryDesc_Color_Date.Clear()
Table_MAINPAGE = New DataTable()
dcol = New DataColumn(" # ")
Table_MAINPAGE.Columns.Add(dcol)
dcol = New DataColumn("Name")
Table_MAINPAGE.Columns.Add(dcol)
myConnection = New SqlConnection(WebConfigurationManager.ConnectionStrings("cvConnectionString").ToString)
MySQL = "select DISTINCT ondate from sList ORDER BY ondate"
myConnection.Open()
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
While myreader.Read
aryDate.Add(myreader(0))
End While
myreader.Close()
pFirst = True
MySQL = "Select fname, lname from Users ORDER BY fname"
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
While myreader.Read
If pFirst = True Then
pFirst = False
'Get distinct Name from database
aryName.Add(myreader(0) & " " & myreader(1))
GoTo NextValue
End If
Name_NotExist = False
Group_NotExist = False
For q As Integer = 0 To aryName.Count - 1
If myreader(0) & " " & myreader(1) <> aryName.Item(q) Then
Name_NotExist = True
Else
Name_NotExist = False
GoTo NextValue
End If
Next
If Name_NotExist = True Then
aryName.Add(myreader(0) & " " & myreader(1))
End If
NextValue:
End While
myreader.Close()
Dim h As Integer = 0
Dim gBool As Boolean = False
'Now add data for dynamic columns
'As first column is increment, as number of data in database
'Let's add some data to the other columns
For k As Integer = 0 To aryName.Count - 1
If Trim(aryName.Item(k).split("-")(1)) = Trim(aryGroup.Item(p)) Then
aryDate_Desc.Clear()
MySQL = "select ondate, description, color from sList where [name] = '" & Trim(aryName.Item(k).ToString) & "' order by ondate"
myCommand = New SqlCommand(MySQL, myConnection)
myreader = myCommand.ExecuteReader
'Create a new row
Row = Table_MAINPAGE.NewRow()
h = h + 1
Row(" # ") = h
Row("Name") = aryName.Item(k).ToString
While myreader.Read
aryDate_Desc.Add(myreader(0) & " - " & myreader(1))
aryDesc_Color_Date.Add(myreader(0) & " -" & myreader(1) & " - " & myreader(3))
End While
'Add the row to the datatable.
Table_MAINPAGE.Rows.Add(Row)
myreader.Close()
End If
Next
GridView1.AutoGenerateColumns = False
Dim NameColumn As New BoundField()
NameColumn.DataField = " # "
NameColumn.HeaderText = " # "
GridView1.Columns.Add(NameColumn)
NameColumn = New BoundField()
NameColumn.DataField = "Name"
NameColumn.HeaderText = "Name"
GridView1.Columns.Add(NameColumn)
' Here is template column portion
Dim TmpCol As New TemplateField()
TmpCol.HeaderText = aryDate.Item(0).ToString
GridView1.Columns.Add(TmpCol)
TmpCol.ItemTemplate = New TemplateHandler(String.Format("<a href=Update.aspx?id=" & "81" & ">" & "work" & "</a>"))
TmpCol = New TemplateField()
TmpCol.HeaderText = aryDate.Item(1).ToString
GridView1.Columns.Add(TmpCol)
TmpCol.ItemTemplate = New TemplateHandler(String.Format("<a href=Update.aspx?id=" & "81" & ">" & "Off" & "</a>"))
TmpCol = New TemplateField()
TmpCol.HeaderText = aryDate.Item(2).ToString
GridView1.Columns.Add(TmpCol)
TmpCol.ItemTemplate = New TemplateHandler(String.Format("<a href=Update.aspx?id=" & "81" & ">" & "Out" & "</a>"))
'Initialize the DataSource
GridView1.DataSource = Table_MAINPAGE
For i As Integer = 0 To GridView1.Columns.Count - 1
GridView1.Columns(i).ItemStyle.Width = 500
Next
'Bind the datatable with the GridView
GridView1.DataBind()
myConnection.Close()
End Sub
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim tCell As TableCell
Dim drv As DataRowView
Dim ob As Object
Dim j As Integer = 1
For i As Integer = 0 To aryDate.Count - 1
j = j + 1
If e.Row.RowType = DataControlRowType.DataRow Then
drv = e.Row.DataItem
ob = drv(aryDate.Item(i))
If ob.ToString.Contains("Work") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.Blue
ElseIf ob.ToString.Contains("Off") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.LightBlue
ElseIf ob.ToString.Contains("Out") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.Gray
ElseIf ob.ToString.Contains("Add New") Then
tCell = e.Row.Cells(j)
tCell.BackColor = Drawing.Color.White
End If
End If
Next
End Sub
End Class
when i use the above code, its creating the hyperlink in label's inside gridview. but the gridview looks like this... its adds same text for the entire columns.
Index-----Name----------12/1/2010-----------12/2/2010------------12/3/2010
1----------John-----------Work-------------------Off----------------------Out
2----------Peter----------Work-------------------Off----------------------Out
But this is the format i need to display the gridview.
Index-----Name----------12/1/2010-----------12/2/2010------------12/3/2010
1----------John-----------Work------------------ADD NEW------------Off
2----------Peter----------ADD NEW-----------Out----------------------ADD NEW
is it possible to display it like that?
i tried this code too.. but same result
Dim myLink As New HyperLink
myLink.Text = columnText
myLink.NavigateUrl = "Update.aspx?id=" & columnID
container.Controls.Add(myLink)
Dim TmpCol As New TemplateField()
TmpCol.HeaderText = aryDate.Item(0).ToString
GridView1.Columns.Add(TmpCol)
TmpCol.ItemTemplate = New TemplateHandler(81,"my Link Text")
If you have any idea, how to do this, please let me know.
Thanks in advance....