By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,298 Members | 695 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,298 IT Pros & Developers. It's quick & easy.

Create Dynamic Hyperlink to GridView VB \ ASP.NET

100+
P: 115
i'm using VB \ ASP.NET.

i'm trying to display some values in Gridview. but i don't know how many columns i need to display. at run time only i will come to know how many columns i need to display. it depends up on the values from Database. so i'm creating columns dynamically and adding to gridview. till that it works fine. but i want some columns value to be hyperlink. so i need to create some columns hyperlink dynamically.

Database table looks like this
Index-------Name--------------Date-----------------Description
1------------John----------------12/1/2010---------- Work
2------------Peter---------------12/2/2010---------- Out
3------------John----------------12/3/2010---------- Off
depends upon the "Distinct Date", the number of columns in gridview is created dynamically.

Gridview looks like this
Index-----Name----------12/1/2010-------12/2/2010----------12/3/2010
1----------John-----------Work--------------ADD NEW----------Off
2----------Peter----------ADD NEW---------Out-----------------ADD NEW
for all the date columns i want to create dynamic hyperlink.
for example : if i need to make any chnage for 12/1/2010 john, i can click hyperlink "Work" and update the information. and if i need to add some description for 12/2/2010 John, i can click hyperlink "Add New" and add description for that date.
Codes
Expand|Select|Wrap|Line Numbers
  1. Sub Display_Table
  2.         Dim Table_MAINPAGE As DataTable
  3.         Dim Row As DataRow
  4.         Dim dcol As DataColumn
  5.         Dim myConnection As SqlConnection
  6.         Dim MySQL As String
  7.         Dim myCommand As SqlCommand
  8.         Dim myreader As SqlDataReader
  9.         Dim pFirst As Boolean = True
  10.         Dim Name_NotExist, Group_NotExist As Boolean
  11.  
  12.         aryDate.Clear()
  13.         aryName.Clear()
  14.         aryDate_Desc.Clear()
  15.  
  16.         Table_MAINPAGE = New DataTable()
  17.  
  18.         dcol = New DataColumn(" # ")
  19.         Table_MAINPAGE.Columns.Add(dcol)
  20.  
  21.         dcol = New DataColumn("Name")
  22.         Table_MAINPAGE.Columns.Add(dcol)
  23.  
  24.         myConnection = New SqlConnection(WebConfigurationManager.ConnectionStrings("cvConnectionString").ToString)
  25.         MySQL = "select DISTINCT ondate from sList ORDER BY ondate"
  26.         myConnection.Open()
  27.         myCommand = New SqlCommand(MySQL, myConnection)
  28.         myreader = myCommand.ExecuteReader
  29.         While myreader.Read
  30.             'Get distinct Date from database
  31.             aryDate.Add(myreader(0))
  32.             dcol = New DataColumn(Trim(myreader(0)))
  33.             Table_MAINPAGE.Columns.Add(dcol)
  34.         End While
  35.         myreader.Close()
  36.  
  37.         pFirst = True
  38.         MySQL = "Select fname, lname from Users ORDER BY fname"
  39.         myCommand = New SqlCommand(MySQL, myConnection)
  40.         myreader = myCommand.ExecuteReader
  41.         While myreader.Read
  42.             If pFirst = True Then
  43.                 pFirst = False
  44.                 'Get distinct Name from database
  45.                 aryName.Add(myreader(0) & " " & myreader(1))
  46.                 GoTo NextValue
  47.             End If
  48.  
  49.             Name_NotExist = False
  50.             Group_NotExist = False
  51.             For q As Integer = 0 To aryName.Count - 1
  52.                 If myreader(0) & " " & myreader(1) <> aryName.Item(q) Then
  53.                     Name_NotExist = True
  54.                 Else
  55.                     Name_NotExist = False
  56.                     GoTo NextValue
  57.                 End If
  58.             Next
  59.             If Name_NotExist = True Then
  60.                 aryName.Add(myreader(0) & " " & myreader(1))
  61.             End If
  62. NextValue:
  63.         End While
  64.         myreader.Close()
  65.  
  66.  
  67.         Dim h As Integer = 0
  68.         Dim gBool As Boolean = False
  69.         'Now add data for dynamic columns
  70.         'As first column is increment, as number of data in database
  71.         'Let's add some data to the other columns
  72.         For k As Integer = 0 To aryName.Count - 1
  73.             aryDate_Desc.Clear()
  74.             MySQL = "select ondate, description from sList where [name] = '" & Trim(aryName.Item(k).ToString) & "' order by ondate"
  75.             myCommand = New SqlCommand(MySQL, myConnection)
  76.             myreader = myCommand.ExecuteReader
  77.             'Create a new row
  78.             Row = Table_MAINPAGE.NewRow()
  79.             h = h + 1
  80.             Row(" # ") = h
  81.             Row("Name") = aryName.Item(k).ToString
  82.             While myreader.Read
  83.                 aryDate_Desc.Add(myreader(0) & " - " & myreader(1))
  84.             End While
  85.             For i As Integer = 0 To aryDate.Count - 1
  86.                 gBool = False
  87.                 For j As Integer = 0 To aryDate_Desc.Count - 1
  88.                     If Trim(aryDate.Item(i)) = Trim(aryDate_Desc.Item(j).Split("-")(0)) Then
  89.                         'Initialize the row data.
  90.                         Row(Trim(aryDate.Item(i))) = Trim(aryDate_Desc.Item(j).Split("-")(1))
  91.                         gBool = True
  92.                         GoTo NextArrayValue
  93.                     End If
  94.                 Next
  95. NextArrayValue:
  96.                 If gBool <> True Then
  97.                     Row(Trim(aryDate.Item(i))) = "Add New"
  98.                 End If
  99.             Next
  100.             'Add the row to the datatable.
  101.             Table_MAINPAGE.Rows.Add(Row)
  102.             myreader.Close()
  103.         Next
  104.  
  105.         'Initialize the DataSource
  106.         GridView1.DataSource = Table_MAINPAGE
  107.  
  108.         For i As Integer = 0 To GridView1.Columns.Count - 1
  109.             GridView1.Columns(i).ItemStyle.Width = 500
  110.         Next
  111.  
  112.         'Bind the datatable with the GridView
  113.         GridView1.DataBind()
  114.  
  115.         myConnection.Close()
  116. End Sub
  117.  
How can i create dynamic hyperlinks for the date columns.....

if you have any idea, how to do this, please help me. if you can some example, that's will be great.

Thanks in advance.
Dec 6 '10 #1
Share this Question
Share on Google+
8 Replies

P: 22
To create actions in gridview, you need to have templatefield. For dynamic templatefields you need to implement 'ITemplate' interface.

I dont know whether URL reference is allowed in this forum. but still to help, hit "Dynamically Adding Template Columns to a GridView" in google search, you will land up in the right example i guess.
Dec 8 '10 #2

Frinavale
Expert Mod 5K+
P: 9,731
It's not simple to add dynamic columns to a GridView.
As @aspdotnetuser has suggested, you need to code a class that implements the ITemplate interface. The purpose of this class is to allow you to specify what controls are used as a template for the dynamic column that you wish to create. This is where you will specify that you want to use a LinkButton or HyperLink control for the data the column is bound to.

Please remember that events are fired and if the parent doesn't handle the event then the event will be lost. So, if you are adding LinkButtons to dynamic column, remember that the click event for the LinkButton should be handled by the class that implements the ITemplate interface....if you want the page code to handle the event, then your ITemplate class has to re-raise the event for the parent to use.

If you are simply using Hyperlinks, then there are no server-side events that will fire and things will be easier for you.

To get you pointed in the right direction, I recommend that you familiarize yourself with how to use dynamic controls in ASP.NET, the INamingContainer Interface and of course also the ITemplate Interface.
Dec 9 '10 #3

100+
P: 115
Thanks aspdotnetuser and Frinavale for your reply....

i will try the templatefields and let you guys know about it... i'm not familiar with templatefields.... let me give a try....

created a small sample application of what i'm doing and i added that code here...

just to make sure whether this is possible to create hyperlink dynamically for this...

at run time only i will come to know how many columns i need to display. it depends up on the values from Database. so i'm creating columns dynamically and adding to gridview. till that it works fine. but i want some columns value to be hyperlink. so i need to create some columns hyperlink dynamically.


Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" MasterPageFile="~/Master/Clearview.master" AutoEventWireup="false" CodeFile="SList.aspx.vb" Inherits="Issues" title="List - Home Page" %>
  2.  
  3.  
  4. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  5.     <div style ="width:800px; overflow-x:scroll;">
  6.      <asp:GridView ID="GridView1" runat="server" Width="800px" CellPadding="4" EnableTheming="True" ForeColor="#333333" GridLines="None" > 
  7.         <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  8.         <RowStyle BackColor="#EFF3FB" />
  9.         <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
  10.         <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  11.         <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  12.          <EditRowStyle BackColor="#2461BF" />
  13.          <AlternatingRowStyle BackColor="White" />
  14.     </asp:GridView>
  15.    </div>
  16. </asp:Content>
  17.  
Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.IO
  3. Imports System.Data
  4. Imports System.Data.SqlClient
  5. Imports System.Web.Configuration
  6.  
  7.  
  8. Partial Class Issues
  9.     Inherits System.Web.UI.Page
  10.  
  11.     Dim aryDate, aryName, aryDate_Desc, aryDesc_Color_Date As New ArrayList
  12.  
  13.  
  14.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  15.         Dim Table_MAINPAGE As DataTable
  16.         Dim Row As DataRow
  17.         Dim dcol As DataColumn
  18.         Dim myConnection As SqlConnection
  19.         Dim MySQL As String
  20.         Dim myCommand As SqlCommand
  21.         Dim myreader As SqlDataReader
  22.         Dim pFirst As Boolean = True
  23.         Dim Name_NotExist, Group_NotExist As Boolean
  24.  
  25.         aryDate.Clear()
  26.         aryName.Clear()
  27.         aryDate_Desc.Clear()
  28.         aryDesc_Color_Date.Clear()
  29.  
  30.         Table_MAINPAGE = New DataTable()
  31.  
  32.         dcol = New DataColumn(" # ")
  33.         Table_MAINPAGE.Columns.Add(dcol)
  34.  
  35.         dcol = New DataColumn("Name")
  36.         Table_MAINPAGE.Columns.Add(dcol)
  37.  
  38.         myConnection = New SqlConnection(WebConfigurationManager.ConnectionStrings("cvConnectionString").ToString)
  39.         MySQL = "select DISTINCT ondate from sList ORDER BY ondate"
  40.         myConnection.Open()
  41.         myCommand = New SqlCommand(MySQL, myConnection)
  42.         myreader = myCommand.ExecuteReader
  43.         While myreader.Read
  44.             aryDate.Add(myreader(0))
  45.             dcol = New DataColumn(Trim(myreader(0)))
  46.             Table_MAINPAGE.Columns.Add(dcol)
  47.         End While
  48.         myreader.Close()
  49.  
  50.         pFirst = True
  51.         MySQL = "Select fname, lname from Users ORDER BY fname"
  52.         myCommand = New SqlCommand(MySQL, myConnection)
  53.         myreader = myCommand.ExecuteReader
  54.         While myreader.Read
  55.             If pFirst = True Then
  56.                 pFirst = False
  57.                 'Get distinct Name from database
  58.                 aryName.Add(myreader(0) & " " & myreader(1))
  59.                 GoTo NextValue
  60.             End If
  61.  
  62.             Name_NotExist = False
  63.             Group_NotExist = False
  64.             For q As Integer = 0 To aryName.Count - 1
  65.                 If myreader(0) & " " & myreader(1) <> aryName.Item(q) Then
  66.                     Name_NotExist = True
  67.                 Else
  68.                     Name_NotExist = False
  69.                     GoTo NextValue
  70.                 End If
  71.             Next
  72.             If Name_NotExist = True Then
  73.                 aryName.Add(myreader(0) & " " & myreader(1))
  74.             End If
  75. NextValue:
  76.         End While
  77.         myreader.Close()
  78.  
  79.  
  80.         Dim h As Integer = 0
  81.         Dim gBool As Boolean = False
  82.         'Now add data for dynamic columns
  83.         'As first column is increment, as number of data in database
  84.         'Let's add some data to the other columns
  85.         For k As Integer = 0 To aryName.Count - 1
  86.             If Trim(aryName.Item(k).split("-")(1)) = Trim(aryGroup.Item(p)) Then
  87.                 aryDate_Desc.Clear()
  88.                 MySQL = "select ondate, description, color from sList where [name] = '" & Trim(aryName.Item(k).ToString) & "' order by ondate"
  89.                 myCommand = New SqlCommand(MySQL, myConnection)
  90.                 myreader = myCommand.ExecuteReader
  91.                 'Create a new row
  92.                 Row = Table_MAINPAGE.NewRow()
  93.                 h = h + 1
  94.                 Row(" # ") = h
  95.                 Row("Name") = aryName.Item(k).ToString
  96.                 While myreader.Read
  97.                     aryDate_Desc.Add(myreader(0) & " - " & myreader(1))
  98.                     aryDesc_Color_Date.Add(myreader(0) & " -" & myreader(1) & " - " & myreader(3))
  99.                 End While
  100.                 For i As Integer = 0 To aryDate.Count - 1
  101.                     gBool = False
  102.                     For j As Integer = 0 To aryDate_Desc.Count - 1
  103.                         If Trim(aryDate.Item(i)) = Trim(aryDate_Desc.Item(j).Split("-")(0)) Then
  104.                             'Initialize the row data.
  105.                             Row(Trim(aryDate.Item(i))) = Trim(aryDate_Desc.Item(j).Split("-")(1))
  106.                             gBool = True
  107.                             GoTo NextArrayValue
  108.                         End If
  109.                     Next
  110. NextArrayValue:
  111.                     If gBool <> True Then
  112.                         Row(Trim(aryDate.Item(i))) = "Add New"
  113.                     End If
  114.                 Next
  115.                 'Add the row to the datatable.
  116.                 Table_MAINPAGE.Rows.Add(Row)
  117.                 myreader.Close()
  118.             End If
  119.         Next
  120.  
  121.         'Initialize the DataSource
  122.         GridView1.DataSource = Table_MAINPAGE
  123.  
  124.         For i As Integer = 0 To GridView1.Columns.Count - 1
  125.             GridView1.Columns(i).ItemStyle.Width = 500
  126.         Next
  127.  
  128.         'Bind the datatable with the GridView
  129.         GridView1.DataBind()
  130.  
  131.         myConnection.Close()
  132.     End Sub
  133.  
  134.     Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
  135.         Dim tCell As TableCell
  136.         Dim drv As DataRowView
  137.         Dim ob As Object
  138.         Dim j As Integer = 1
  139.  
  140.         For i As Integer = 0 To aryDate.Count - 1
  141.             j = j + 1
  142.             If e.Row.RowType = DataControlRowType.DataRow Then
  143.                 drv = e.Row.DataItem
  144.                 ob = drv(aryDate.Item(i))
  145.                 If ob.ToString.Contains("Work") Then
  146.                     tCell = e.Row.Cells(j)
  147.                     tCell.BackColor = Drawing.Color.Blue
  148.                 ElseIf ob.ToString.Contains("Off") Then
  149.                     tCell = e.Row.Cells(j)
  150.                     tCell.BackColor = Drawing.Color.LightBlue
  151.                 ElseIf ob.ToString.Contains("Out") Then
  152.                     tCell = e.Row.Cells(j)
  153.                     tCell.BackColor = Drawing.Color.Gray
  154.                 ElseIf ob.ToString.Contains("Add New") Then
  155.                     tCell = e.Row.Cells(j)
  156.                     tCell.BackColor = Drawing.Color.White
  157.                 End If
  158.             End If
  159.         Next
  160.     End Sub
  161.  
  162.     Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
  163.         'If (e.Row.RowType = DataControlRowType.DataRow) Then
  164.         '    Dim link As HyperLink = New HyperLink()
  165.         '    link.Text = e.Row.DataItem
  166.         '    link.NavigateUrl = "Update.aspx" + e.Row.DataItem
  167.         '    ' link.NavigateUrl = "Navigate somewhere based on data: " + e.Row.DataItem
  168.         '    e.Row.Cells(2).Controls.Add(link)
  169.         'End If
  170.     End Sub
  171.  
  172. End Class
  173.  
my Database table looks like this

Index-------Name--------------Date-----------------Description
1------------John----------------12/1/2010---------- Work
2------------Peter---------------12/2/2010---------- Out
3------------John----------------12/3/2010---------- Off
when this code runs, gridview display like this

Index-----Name----------12/1/2010-----------12/2/2010------------12/3/2010
1----------John-----------Work------------------ADD NEW------------Off
2----------Peter----------ADD NEW-----------Out----------------------ADD NEW
that's the way i want it to be displayed....

but the next thing i want is to make all the Date coumns to displayed as hyperlink.


for example :
for Work (12/1/2010) - Update.aspx?id=1
for Off (12/3/2010) - Update.aspx?id=3
for Out (12/2/2010) - Update.aspx?id=2
How can i create dynamic hyperlinks for the date columns.....

if you have any idea, how to do this, please please please help me. if you have some example, that's will be great.

Thanks in advance.
Dec 9 '10 #4

Frinavale
Expert Mod 5K+
P: 9,731
Please read through the links I posted :)

You will create a "template" to use for the column.
This template will contain a HyperLink.
The Hyperlink will be bound to the URL retrieved from the database.

In order to create a dynamic template column you need to write a class that will be the dynamic template column. This class implements the ITemplate interface that we were talking about.

When you retrieve the data from the database, you will dynamically create the number of columns that you need to use to display your data. When you create these columns, one of them will be your custom template column that contains the link that is bound to the data retrieved from the database.

You have to create your columns in the Page Init event if you expect to be able to handle any events that are fired from controls within the dynamic columns.

This event occurs every page request, it occurs before your page's ViewState is loaded (before the Page Load event) so you need to keep track of the number of columns to create using a cookie or a hidden field.


-Frinny
Dec 9 '10 #5

100+
P: 115
Thanks Frinavale for your reply....

i checked the TemplateField...

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,
Expand|Select|Wrap|Line Numbers
  1. Imports System.Web.UI
  2. Imports System.Web.UI.WebControls
  3. Imports system.IO
  4.  
  5.  
  6. Public Class TemplateHandler
  7.     Implements ITemplate
  8.  
  9.     Private columnID, columnText As String
  10.  
  11.     Public Sub New(ByVal colText As String)
  12.         columnText = colText
  13.     End Sub
  14.  
  15.     Private Sub ITemplate_InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
  16.         Dim lbl As New Label()
  17.         lbl.Text = columnText
  18.         container.Controls.Add(lbl)
  19.     End Sub
  20.  
  21. End Class
  22.  
Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.IO
  3. Imports System.Data
  4. Imports System.Data.SqlClient
  5. Imports System.Web.Configuration
  6.  
  7.  
  8. Partial Class Issues
  9.     Inherits System.Web.UI.Page
  10.  
  11.     Dim aryDate, aryName, aryDate_Desc, aryDesc_Color_Date As New ArrayList
  12.  
  13.  
  14.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  15.         Dim Table_MAINPAGE As DataTable
  16.         Dim Row As DataRow
  17.         Dim dcol As DataColumn
  18.         Dim myConnection As SqlConnection
  19.         Dim MySQL As String
  20.         Dim myCommand As SqlCommand
  21.         Dim myreader As SqlDataReader
  22.         Dim pFirst As Boolean = True
  23.         Dim Name_NotExist, Group_NotExist As Boolean
  24.  
  25.         aryDate.Clear()
  26.         aryName.Clear()
  27.         aryDate_Desc.Clear()
  28.         aryDesc_Color_Date.Clear()
  29.  
  30.         Table_MAINPAGE = New DataTable()
  31.  
  32.         dcol = New DataColumn(" # ")
  33.         Table_MAINPAGE.Columns.Add(dcol)
  34.  
  35.         dcol = New DataColumn("Name")
  36.         Table_MAINPAGE.Columns.Add(dcol)
  37.  
  38.         myConnection = New SqlConnection(WebConfigurationManager.ConnectionStrings("cvConnectionString").ToString)
  39.         MySQL = "select DISTINCT ondate from sList ORDER BY ondate"
  40.         myConnection.Open()
  41.         myCommand = New SqlCommand(MySQL, myConnection)
  42.         myreader = myCommand.ExecuteReader
  43.         While myreader.Read
  44.             aryDate.Add(myreader(0))
  45.         End While
  46.         myreader.Close()
  47.  
  48.         pFirst = True
  49.         MySQL = "Select fname, lname from Users ORDER BY fname"
  50.         myCommand = New SqlCommand(MySQL, myConnection)
  51.         myreader = myCommand.ExecuteReader
  52.         While myreader.Read
  53.             If pFirst = True Then
  54.                 pFirst = False
  55.                 'Get distinct Name from database
  56.                 aryName.Add(myreader(0) & " " & myreader(1))
  57.                 GoTo NextValue
  58.             End If
  59.  
  60.             Name_NotExist = False
  61.             Group_NotExist = False
  62.             For q As Integer = 0 To aryName.Count - 1
  63.                 If myreader(0) & " " & myreader(1) <> aryName.Item(q) Then
  64.                     Name_NotExist = True
  65.                 Else
  66.                     Name_NotExist = False
  67.                     GoTo NextValue
  68.                 End If
  69.             Next
  70.             If Name_NotExist = True Then
  71.                 aryName.Add(myreader(0) & " " & myreader(1))
  72.             End If
  73. NextValue:
  74.         End While
  75.         myreader.Close()
  76.  
  77.  
  78.         Dim h As Integer = 0
  79.         Dim gBool As Boolean = False
  80.         'Now add data for dynamic columns
  81.         'As first column is increment, as number of data in database
  82.         'Let's add some data to the other columns
  83.         For k As Integer = 0 To aryName.Count - 1
  84.             If Trim(aryName.Item(k).split("-")(1)) = Trim(aryGroup.Item(p)) Then
  85.                 aryDate_Desc.Clear()
  86.                 MySQL = "select ondate, description, color from sList where [name] = '" & Trim(aryName.Item(k).ToString) & "' order by ondate"
  87.                 myCommand = New SqlCommand(MySQL, myConnection)
  88.                 myreader = myCommand.ExecuteReader
  89.                 'Create a new row
  90.                 Row = Table_MAINPAGE.NewRow()
  91.                 h = h + 1
  92.                 Row(" # ") = h
  93.                 Row("Name") = aryName.Item(k).ToString
  94.                 While myreader.Read
  95.                     aryDate_Desc.Add(myreader(0) & " - " & myreader(1))
  96.                     aryDesc_Color_Date.Add(myreader(0) & " -" & myreader(1) & " - " & myreader(3))
  97.                 End While
  98.                 'Add the row to the datatable.
  99.                 Table_MAINPAGE.Rows.Add(Row)
  100.                 myreader.Close()
  101.             End If
  102.         Next
  103.  
  104.         GridView1.AutoGenerateColumns = False
  105.  
  106.         Dim NameColumn As New BoundField()
  107.         NameColumn.DataField = " # "
  108.         NameColumn.HeaderText = " # "
  109.         GridView1.Columns.Add(NameColumn)
  110.  
  111.         NameColumn = New BoundField()
  112.         NameColumn.DataField = "Name"
  113.         NameColumn.HeaderText = "Name"
  114.         GridView1.Columns.Add(NameColumn)
  115.  
  116.         ' Here is template column portion
  117.         Dim TmpCol As New TemplateField()
  118.         TmpCol.HeaderText = aryDate.Item(0).ToString
  119.         GridView1.Columns.Add(TmpCol)
  120.         TmpCol.ItemTemplate = New TemplateHandler(String.Format("<a href=Update.aspx?id=" & "81" & ">" & "work" & "</a>"))
  121.  
  122.         TmpCol = New TemplateField()
  123.         TmpCol.HeaderText = aryDate.Item(1).ToString
  124.         GridView1.Columns.Add(TmpCol)
  125.         TmpCol.ItemTemplate = New TemplateHandler(String.Format("<a href=Update.aspx?id=" & "81" & ">" & "Off" & "</a>"))
  126.  
  127.         TmpCol = New TemplateField()
  128.         TmpCol.HeaderText = aryDate.Item(2).ToString
  129.         GridView1.Columns.Add(TmpCol)
  130.         TmpCol.ItemTemplate = New TemplateHandler(String.Format("<a href=Update.aspx?id=" & "81" & ">" & "Out" & "</a>"))
  131.  
  132.         'Initialize the DataSource
  133.         GridView1.DataSource = Table_MAINPAGE
  134.  
  135.         For i As Integer = 0 To GridView1.Columns.Count - 1
  136.             GridView1.Columns(i).ItemStyle.Width = 500
  137.         Next
  138.  
  139.         'Bind the datatable with the GridView
  140.         GridView1.DataBind()
  141.  
  142.         myConnection.Close()
  143.     End Sub
  144.  
  145.     Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
  146.         Dim tCell As TableCell
  147.         Dim drv As DataRowView
  148.         Dim ob As Object
  149.         Dim j As Integer = 1
  150.  
  151.         For i As Integer = 0 To aryDate.Count - 1
  152.             j = j + 1
  153.             If e.Row.RowType = DataControlRowType.DataRow Then
  154.                 drv = e.Row.DataItem
  155.                 ob = drv(aryDate.Item(i))
  156.                 If ob.ToString.Contains("Work") Then
  157.                     tCell = e.Row.Cells(j)
  158.                     tCell.BackColor = Drawing.Color.Blue
  159.                 ElseIf ob.ToString.Contains("Off") Then
  160.                     tCell = e.Row.Cells(j)
  161.                     tCell.BackColor = Drawing.Color.LightBlue
  162.                 ElseIf ob.ToString.Contains("Out") Then
  163.                     tCell = e.Row.Cells(j)
  164.                     tCell.BackColor = Drawing.Color.Gray
  165.                 ElseIf ob.ToString.Contains("Add New") Then
  166.                     tCell = e.Row.Cells(j)
  167.                     tCell.BackColor = Drawing.Color.White
  168.                 End If
  169.             End If
  170.         Next
  171.     End Sub
  172.  
  173. End Class
  174.  
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
Expand|Select|Wrap|Line Numbers
  1. Dim myLink As New HyperLink
  2. myLink.Text = columnText
  3. myLink.NavigateUrl = "Update.aspx?id=" & columnID
  4. container.Controls.Add(myLink)
  5.  
Expand|Select|Wrap|Line Numbers
  1. Dim TmpCol As New TemplateField()
  2. TmpCol.HeaderText = aryDate.Item(0).ToString
  3. GridView1.Columns.Add(TmpCol)
  4. TmpCol.ItemTemplate = New TemplateHandler(81,"my Link Text")
  5.  
If you have any idea, how to do this, please let me know.

Thanks in advance....
Dec 15 '10 #6

100+
P: 115
Thanks Gary for your reply and for the sample example.

Sorry for this late reply... actually i was trying your codes and made some changes and it start displaying the fields in gridview. but hyperlink is not working correctly...

The code used...
Expand|Select|Wrap|Line Numbers
  1.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.  
  3.         Dim h As Integer = 0
  4.         Dim gBool As Boolean = False
  5.         'Now add data for dynamic columns
  6.         'As first column is increment, as number of data in database
  7.         'Let's add some data to the other columns
  8.         For k As Integer = 0 To aryName.Count - 1
  9.             If Trim(aryName.Item(k).split("-")(1)) = Trim(aryGroup.Item(p)) Then
  10.                 aryDate_Desc.Clear()
  11.                 MySQL = "select ondate, description, color, recno from sList where [name] = '" & Trim(aryName.Item(k).ToString) & "' order by ondate"
  12.                 myCommand = New SqlCommand(MySQL, myConnection)
  13.                 myreader = myCommand.ExecuteReader
  14.                 'Create a new row
  15.                 Row = Table_MAINPAGE.NewRow()
  16.                 h = h + 1
  17.                 Row(" # ") = h
  18.                 Row("Name") = aryName.Item(k).ToString
  19.                 While myreader.Read
  20.                     aryDate_Desc.Add(myreader(0) & " - " & myreader(1))
  21.                     aryDesc_Color_Date.Add(myreader(0) & " - " & myreader(1) & " - " & myreader(2))
  22.                     aryHyperLinkIndex.Add(myreader(0) & " - " & myreader(1) & " - " & myreader(3) & " - " & Trim(aryName.Item(k).ToString))
  23.                 End While
  24.     For i As Integer = 0 To aryDate.Count - 1
  25.                     gBool = False
  26.                     For j As Integer = 0 To aryDate_Desc.Count - 1
  27.                         If Trim(aryDate.Item(i)) = Trim(aryDate_Desc.Item(j).Split("-")(0)) Then
  28.                             'Initialize the row data.
  29.                             Row(Trim(aryDate.Item(i))) = String.Format("<a href='Update.aspx?id=81'>" & Trim(aryDate_Desc.Item(j).Split(" - ")(1)) & "</a>") 
  30.                             gBool = True
  31.                             GoTo NextArrayValue
  32.                         End If
  33.                     Next
  34. NextArrayValue:
  35.                     If gBool <> True Then
  36.                         Row(Trim(aryDate.Item(i))) = "Add New"
  37.             aryHyperLinkIndex.Add(Trim(aryDate.Item(i)) & " - " & "Add New" & " - " & "0" & " - " & Trim(aryName.Item(k).split("-")(0)).ToString)
  38.                     End If
  39.                 Next
  40.  
  41.                 'Add the row to the datatable.
  42.                 Table_MAINPAGE.Rows.Add(Row)
  43.                 myreader.Close()
  44.             End If
  45.         Next
  46.             'Create a new row
  47.             Row = Table_MAINPAGE.NewRow()
  48.             Row(" # ") = ""
  49.             Row("Name") = ""
  50.             For i As Integer = 0 To aryDate.Count - 1
  51.                 'Initialize the row data.
  52.                 Row(Trim(aryDate.Item(i))) = ""
  53.             Next
  54.             'Add the row to the datatable.
  55.             Table_MAINPAGE.Rows.Add(Row)
  56.         Next
  57.  
  58.         'Initialize the DataSource
  59.         GridView1.DataSource = Table_MAINPAGE
  60.  
  61.         For i As Integer = 0 To GridView1.Columns.Count - 1
  62.             GridView1.Columns(i).ItemStyle.Width = 500
  63.         Next
  64.  
  65.         'Bind the datatable with the GridView
  66.         GridView1.DataBind()
  67.  
  68.         myConnection.Close()
  69.     End Sub
  70.  
  71.     Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
  72.         Dim tCell As TableCell
  73.         Dim drv As DataRowView
  74.         Dim ob As Object
  75.         Dim j As Integer = 1
  76.  
  77.         For i As Integer = 0 To aryDate.Count - 1
  78.             j = j + 1
  79.             If e.Row.RowType = DataControlRowType.DataRow Then
  80.                 drv = e.Row.DataItem
  81.                 ob = drv(aryDate.Item(i))
  82.                 If ob.ToString.Contains("Work") Then
  83.                     tCell = e.Row.Cells(j)
  84.                     tCell.BackColor = Drawing.Color.Blue
  85.                 ElseIf ob.ToString.Contains("Off") Then
  86.                     tCell = e.Row.Cells(j)
  87.                     tCell.BackColor = Drawing.Color.LightBlue
  88.                 ElseIf ob.ToString.Contains("Out") Then
  89.                     tCell = e.Row.Cells(j)
  90.                     tCell.BackColor = Drawing.Color.Gray
  91.                 ElseIf ob.ToString.Contains("Add New") Then
  92.                     tCell = e.Row.Cells(j)
  93.                     tCell.BackColor = Drawing.Color.White
  94.                 End If
  95.             End If
  96.         Next
  97.     End Sub
  98.  
  99. End Class
  100.  
  101. Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
  102.  
  103.         If e.Row.RowType = DataControlRowType.DataRow Then
  104.  
  105.             For i As Integer = 0 To aryDate.Count - 1
  106.                 For k As Integer = 0 To aryHyperLinkIndex.Count - 1
  107.                     If Trim(aryDate.Item(i)) = Trim(aryHyperLinkIndex.Item(k).split("-")(0)) Then
  108.                         If Trim(e.Row.Cells(2).Text) = Trim(aryHyperLinkIndex.Item(k).Split("-")(1)) Then
  109.                             If Trim(e.Row.Cells(1).Text) = Trim(aryHyperLinkIndex.Item(k).split("-")(3)) Then
  110.                                 e.Row.Cells(2).Text = e.Row.Cells(2).Text
  111.                                 If Trim(e.Row.Cells(2).Text) = "Add New" Then
  112.                                     Dim link As HyperLink = New HyperLink()
  113.                                     link.Text = e.Row.Cells(2).Text
  114.                                     link.NavigateUrl = "AddValues.aspx?DateValue=" & Trim(aryDate.Item(i)) & "&NameValue=" & m(aryHyperLinkIndex.Item(k).split("-")(3))
  115.                                     e.Row.Cells(2).Controls.Add(link)
  116.                                 Else
  117.                                     Dim link As HyperLink = New HyperLink()
  118.                                     link.Text = e.Row.Cells(2).Text
  119.                                     link.NavigateUrl = "ViewSchedule.aspx?ViewUpdateValue=" & Trim(aryHyperLinkIndex.Item(k).Split("-")(2))
  120.                                     e.Row.Cells(2).Controls.Add(link)
  121.                                 End If
  122.                                 Exit For
  123.                             End If
  124.                         End If
  125.                     End If
  126.                 Next
  127.             Next
  128.  
  129.            For i As Integer = 0 To aryDate.Count - 1
  130.                     For k As Integer = 0 To aryHyperLinkIndex.Count - 1
  131.                         If Trim(aryDate.Item(i)) = Trim(aryHyperLinkIndex.Item(k).split("-")(0)) Then
  132.                             If Trim(e.Row.Cells(3).Text) = Trim(aryHyperLinkIndex.Item(k).Split("-")(1)) Then
  133.                                 If Trim(e.Row.Cells(1).Text) = Trim(aryHyperLinkIndex.Item(k).split("-")(3)) Then
  134.                                     e.Row.Cells(3).Text = e.Row.Cells(3).Text
  135.                                     If Trim(e.Row.Cells(3).Text) = "Add New" Then
  136.                                         Dim link As HyperLink = New HyperLink()
  137.                                         link.Text = e.Row.Cells(3).Text
  138.                                         link.NavigateUrl = "AddValues.aspx?DateValue=" & Trim(aryDate.Item(i)) & "&NameValue=" & Trim(aryHyperLinkIndex.Item(k).split("-")(3))
  139.                                         e.Row.Cells(3).Controls.Add(link)
  140.                                     Else
  141.                                         Dim link As HyperLink = New HyperLink()
  142.                                         link.Text = e.Row.Cells(3).Text
  143.                                         link.NavigateUrl = "ViewSchedule.aspx?ViewUpdateValue=" & Trim(aryHyperLinkIndex.Item(k).Split("-")(2))
  144.                                         e.Row.Cells(3).Controls.Add(link)
  145.                                     End If
  146.                                     Exit For
  147.                                 End If
  148.                             End If
  149.                         End If
  150.                     Next
  151.            Next
  152.  
  153.          End If
  154.         myWrite.Close()
  155.     End Sub
  156.  
for "Add New", when i try to hit the hyperlink, its taking all the "Add New" hyperlinks to a single index values. its not taking to corresponding index value.

for example: First Add New - index 10
Second Add New - index 12
Third Add New - index 14

while creating hyperlink its assigning correct index number to each hyperlink. but when i hit hyperlink, it always take index 14. always the last index is assigning to all the Add New's.

is there anything wrong in this code? if you have any idea, why hyperlink is not getting the correct index, then can you please help me to figure this out...

Thanks in advance...
Jan 3 '11 #7

100+
P: 115
Sorry.... that was a mistake i did while typing...

Actually instead of this codes

Row(Trim(aryDate.Item(i))) = String.Format("<a href='Update.aspx?id=81'>" & Trim(aryDate_Desc.Item(j).Split(" - ")(1)) & "</a>")

i'm using this

Row(Trim(aryDate.Item(i))) = Trim(aryDate_Desc.Item(j).Split("-")(1))
while typing in this forum, i made that mistake...

i will give some idea what i'm trying to so with this code...

inside database the data is saved like this
Index-------Name--------------Date-----------------Description
1------------John----------------12/1/2010---------- Work
2------------Peter---------------12/2/2010---------- Out
3------------John----------------12/3/2010---------- Off
and i want the above details to display like this in web page
Index-----Name----------12/1/2010-----------12/2/2010------------12/3/2010
1----------John-----------Work------------------ADD NEW------------Off
2----------Peter----------ADD NEW-----------Out----------------------ADD NEW
Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.  
  3.         Dim h As Integer = 0
  4.         Dim gBool As Boolean = False
  5.         'Now add data for dynamic columns
  6.         For k As Integer = 0 To aryName.Count - 1
  7.             If Trim(aryName.Item(k).split("-")(1)) = Trim(aryGroup.Item(p)) Then
  8.                 aryDate_Desc.Clear()
  9.     'for each name, need to get date, description, color and recno
  10.                 MySQL = "select ondate, description, color, recno from sList where [name] = '" & Trim(aryName.Item(k).ToString) & "' order by ondate"
  11.                 myCommand = New SqlCommand(MySQL, myConnection)
  12.                 myreader = myCommand.ExecuteReader
  13.                 'Create a new row
  14.                 Row = Table_MAINPAGE.NewRow()
  15.     h = h + 1
  16.     'adding values to "#" row.
  17.                 Row(" # ") = h
  18.     'adding values to "Name" row.
  19.                 Row("Name") = aryName.Item(k).ToString
  20.                 While myreader.Read
  21.                     aryDate_Desc.Add(myreader(0) & " - " & myreader(1))
  22.                     'adding date, description, index, name to an array. so while creating hyperlink, we can pass the index values        
  23.                     aryHyperLinkIndex.Add(myreader(0) & " - " & myreader(1) & " - " & myreader(3) & " - " & Trim(aryName.Item(k).ToString))
  24.                 End While
  25.     For i As Integer = 0 To aryDate.Count - 1
  26.                     gBool = False
  27.                     For j As Integer = 0 To aryDate_Desc.Count - 1
  28.                         If Trim(aryDate.Item(i)) = Trim(aryDate_Desc.Item(j).Split("-")(0)) Then
  29.                             'adding value to "Date" row.
  30.                             Row(Trim(aryDate.Item(i))) = Trim(aryDate_Desc.Item(j).Split("-")(1))
  31.                             gBool = True
  32.                             GoTo NextArrayValue
  33.                         End If
  34.                     Next
  35. NextArrayValue:
  36.                     If gBool <> True Then
  37.                         'adding value as "Add New" to "Date" row, if no value is set.
  38.                         Row(Trim(aryDate.Item(i))) = "Add New"
  39.             'adding date, description, index, name to an array. so while creating hyperlink, we can pass the index values
  40.             aryHyperLinkIndex.Add(Trim(aryDate.Item(i)) & " - " & "Add New" & " - " & "0" & " - " & Trim(aryName.Item(k).split("-")(0)).ToString)
  41.                     End If
  42.                 Next
  43.  
  44.                 'Add the row to the datatable.
  45.                 Table_MAINPAGE.Rows.Add(Row)
  46.                 myreader.Close()
  47.             End If
  48.         Next
  49.             'for new group, we need to create empty row (empty row will seperate each group)
  50.             'Create a new row
  51.             Row = Table_MAINPAGE.NewRow()
  52.             Row(" # ") = ""
  53.             Row("Name") = ""
  54.             For i As Integer = 0 To aryDate.Count - 1
  55.                 'Initialize the row data.
  56.                 Row(Trim(aryDate.Item(i))) = ""
  57.             Next
  58.             'Add the row to the datatable.
  59.             Table_MAINPAGE.Rows.Add(Row)
  60.         Next
  61.  
  62.         'Initialize the DataSource
  63.         GridView1.DataSource = Table_MAINPAGE
  64.  
  65.         For i As Integer = 0 To GridView1.Columns.Count - 1
  66.             GridView1.Columns(i).ItemStyle.Width = 500
  67.         Next
  68.  
  69.         'Bind the datatable with the GridView
  70.         GridView1.DataBind()
  71.  
  72.         myConnection.Close()
  73.     End Sub
  74.  
  75. Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
  76.  
  77.         If e.Row.RowType = DataControlRowType.DataRow Then
  78.  
  79.             'checking each Date    
  80.             For i As Integer = 0 To aryDate.Count - 1
  81.                 For k As Integer = 0 To aryHyperLinkIndex.Count - 1
  82.         'if Date and aryHyperLinkIndex are equal
  83.                     If Trim(aryDate.Item(i)) = Trim(aryHyperLinkIndex.Item(k).split("-")(0)) Then
  84.             'if current cell2  (first Date) text is equal to aryHyperLinkIndex description     
  85.                         If Trim(e.Row.Cells(2).Text) = Trim(aryHyperLinkIndex.Item(k).Split("-")(1)) Then
  86.                 'if current cell1 (Name) text is equal to aryHyperLinkIndex name    
  87.                             If Trim(e.Row.Cells(1).Text) = Trim(aryHyperLinkIndex.Item(k).split("-")(3)) Then
  88.                                 e.Row.Cells(2).Text = e.Row.Cells(2).Text
  89.            'if current cell2 (first date) text is equal to "Add New", then create Hyperlink
  90.                                 If Trim(e.Row.Cells(2).Text) = "Add New" Then
  91.                                     Dim link As HyperLink = New HyperLink()
  92.                                     link.Text = e.Row.Cells(2).Text
  93.             'Passing the date and name
  94.                                     link.NavigateUrl = "AddValues.aspx?DateValue=" & Trim(aryDate.Item(i)) & "&NameValue=" & m(aryHyperLinkIndex.Item(k).split("-")(3))
  95.                                     e.Row.Cells(2).Controls.Add(link)
  96.                                 Else
  97.                                     Dim link As HyperLink = New HyperLink()
  98.                                     link.Text = e.Row.Cells(2).Text
  99.             'Passing the index
  100.                                     link.NavigateUrl = "ViewUpdate.aspx?ViewUpdateValue=" & Trim(aryHyperLinkIndex.Item(k).Split("-")(2))
  101.                                     e.Row.Cells(2).Controls.Add(link)
  102.                                 End If
  103.                                 Exit For
  104.                             End If
  105.                         End If
  106.                     End If
  107.                 Next
  108.             Next
  109.  
  110.            'checking each Date    
  111.            For i As Integer = 0 To aryDate.Count - 1
  112.                     For k As Integer = 0 To aryHyperLinkIndex.Count - 1
  113.              'if Date and aryHyperLinkIndex are equal
  114.                         If Trim(aryDate.Item(i)) = Trim(aryHyperLinkIndex.Item(k).split("-")(0)) Then
  115.                'if current cell2  (first Date) text is equal to aryHyperLinkIndex description 
  116.                             If Trim(e.Row.Cells(3).Text) = Trim(aryHyperLinkIndex.Item(k).Split("-")(1)) Then
  117.                                 'if current cell1 (Name) text is equal to aryHyperLinkIndex name    
  118.                                 If Trim(e.Row.Cells(1).Text) = Trim(aryHyperLinkIndex.Item(k).split("-")(3)) Then
  119.                                     e.Row.Cells(3).Text = e.Row.Cells(3).Text
  120.                'if current cell2 (first date) text is equal to "Add New", then create Hyperlink
  121.                                     If Trim(e.Row.Cells(3).Text) = "Add New" Then
  122.                                         Dim link As HyperLink = New HyperLink()
  123.                                         link.Text = e.Row.Cells(3).Text
  124.                                         'Passing the date and name
  125.                                         link.NavigateUrl = "AddValues.aspx?DateValue=" & Trim(aryDate.Item(i)) & "&NameValue=" & Trim(aryHyperLinkIndex.Item(k).split("-")(3))
  126.                                         e.Row.Cells(3).Controls.Add(link)
  127.                                     Else
  128.                                         Dim link As HyperLink = New HyperLink()
  129.                                         link.Text = e.Row.Cells(3).Text
  130.                                         'Passing the index
  131.                                         link.NavigateUrl = "ViewUpdate.aspx?ViewUpdateValue=" & Trim(aryHyperLinkIndex.Item(k).Split("-")(2))
  132.                                         e.Row.Cells(3).Controls.Add(link)
  133.                                     End If
  134.                                     Exit For
  135.                                 End If
  136.                             End If
  137.                         End If
  138.                     Next
  139.            Next
  140.  
  141.          End If
  142.     End Sub
  143.  
GridView1_RowDataBound

aryHyperLinkIndex contain - date, description, index, name of all items in each row.

if Date and aryHyperLinkIndex are equal
if current cell2 (first Date) text is equal to aryHyperLinkIndex description
if current cell1 (Name) text is equal to aryHyperLinkIndex name
if current cell2 (first date) text is equal to "Add New"
then create Hyperlink for Add New and Passing the date and name
else
create HyperLink for Text and Passing the index
for HyperLink Text, its taking the correct index and it works fine.
But for Add New HyperlLink, i'm passing date and name. its getting the correct name. but date is set to the last date.

i check the view source and for Add New, its taking the last Date for all.
<td>1</td><td>John </td><td style="background-color:DarkOrange;"><a href="ViewUpdate.aspx?ViewUpdateValue=102">Work</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&amp;NameValue=John">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&amp;NameValue=John">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&amp;NameValue=John">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&amp;NameValue=John">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&amp;NameValue=John">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&amp;NameValue=John">Add New</a></td>

<td>2</td><td>Peter </td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&amp;NameValue=Peter">Add New</a></td><td style="background-color:OliveDrab;"><a href="ViewUpdate.aspx?ViewUpdateValue=103">Off</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&amp;NameValue=Peter">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&amp;NameValue=Peter">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&amp;NameValue=Peter">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&amp;NameValue=Peter">Add New</a></td><td style="background-color:White;"><a href="AddValues.aspx?DateValue=1/10/2011&amp;NameValue=Peter">Add New</a></td>
is there anything wrong in this code? if you have any idea, why hyperlink is not getting the correct date, then can you please help me to figure this out...

Thanks in advance...
Jan 4 '11 #8

100+
P: 115
Thank you so much for your help to solve this problem...

adding code here. so later if someone have this problem, they can check this.
Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.  
  3.         Dim h As Integer = 0
  4.         Dim gBool As Boolean = False
  5.         'Now add data for dynamic columns
  6.         For k As Integer = 0 To aryName.Count - 1
  7.             If Trim(aryName.Item(k).split("-")(1)) = Trim(aryGroup.Item(p)) Then
  8.                 aryDate_Desc.Clear()
  9.     'for each name, need to get date, description, color and recno
  10.                 MySQL = "select ondate, description, color, recno from sList where [name] = '" & Trim(aryName.Item(k).ToString) & "' order by ondate"
  11.                 myCommand = New SqlCommand(MySQL, myConnection)
  12.                 myreader = myCommand.ExecuteReader
  13.                 'Create a new row
  14.                 Row = Table_MAINPAGE.NewRow()
  15.     h = h + 1
  16.     'adding values to "#" row.
  17.                 Row(" # ") = h
  18.     'adding values to "Name" row.
  19.                 Row("Name") = aryName.Item(k).ToString
  20.                 While myreader.Read
  21.                     aryDate_Desc.Add(myreader(0) & " - " & myreader(1))
  22.                     'adding date, description, index, name to an array. so while creating hyperlink, we can pass the index values        
  23.                     aryHyperLinkIndex.Add(myreader(0) & " - " & myreader(1) & " - " & myreader(3) & " - " & Trim(aryName.Item(k).ToString))
  24.                 End While
  25.     For i As Integer = 0 To aryDate.Count - 1
  26.                     gBool = False
  27.                     For j As Integer = 0 To aryDate_Desc.Count - 1
  28.                         If Trim(aryDate.Item(i)) = Trim(aryDate_Desc.Item(j).Split("-")(0)) Then
  29.                             'adding value to "Date" row.
  30.                             Row(Trim(aryDate.Item(i))) = Trim(aryDate_Desc.Item(j).Split("-")(1))
  31.                             gBool = True
  32.                             GoTo NextArrayValue
  33.                         End If
  34.                     Next
  35. NextArrayValue:
  36.                     If gBool <> True Then
  37.                         'adding value as "Add New" to "Date" row, if no value is set.
  38.                         Row(Trim(aryDate.Item(i))) = "Add New"
  39.             'adding date, description, index, name to an array. so while creating hyperlink, we can pass the index values
  40.             aryHyperLinkIndex.Add(Trim(aryDate.Item(i)) & " - " & "Add New" & " - " & "0" & " - " & Trim(aryName.Item(k).split("-")(0)).ToString)
  41.                     End If
  42.                 Next
  43.  
  44.                 'Add the row to the datatable.
  45.                 Table_MAINPAGE.Rows.Add(Row)
  46.                 myreader.Close()
  47.             End If
  48.         Next
  49.             'for new group, we need to create empty row (empty row will seperate each group)
  50.             'Create a new row
  51.             Row = Table_MAINPAGE.NewRow()
  52.             Row(" # ") = ""
  53.             Row("Name") = ""
  54.             For i As Integer = 0 To aryDate.Count - 1
  55.                 'Initialize the row data.
  56.                 Row(Trim(aryDate.Item(i))) = ""
  57.             Next
  58.             'Add the row to the datatable.
  59.             Table_MAINPAGE.Rows.Add(Row)
  60.         Next
  61.  
  62.         'Initialize the DataSource
  63.         GridView1.DataSource = Table_MAINPAGE
  64.  
  65.         For i As Integer = 0 To GridView1.Columns.Count - 1
  66.             GridView1.Columns(i).ItemStyle.Width = 500
  67.         Next
  68.  
  69.         'Bind the datatable with the GridView
  70.         GridView1.DataBind()
  71.  
  72.         myConnection.Close()
  73.     End Sub
  74.  
  75.  
  76. Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
  77.  
  78.   If e.Row.RowType = DataControlRowType.DataRow Then
  79.  
  80.              'gets first date cell's header    
  81.             Dim Header_Date1 As DataControlFieldHeaderCell = CType(Me.GridView1.HeaderRow.Cells(2), DataControlFieldHeaderCell)
  82.                 For k As Integer = 0 To aryHyperLinkIndex.Count - 1
  83.                      'checks if first date cell's header and AryHyperLinkIndex date are equal
  84.                     If Trim(Header_Date1.Text) = Trim(aryHyperLinkIndex.Item(k).split("-")(0)) Then
  85.                         'if Current cell2  (first Date) text is equal to aryHyperLinkIndex description     
  86.                         If Trim(e.Row.Cells(2).Text) = Trim(aryHyperLinkIndex.Item(k).Split("-")(1)) Then
  87.                             'if current cell1 (Name) text is equal to aryHyperLinkIndex name
  88.                             If Trim(e.Row.Cells(1).Text) = Trim(aryHyperLinkIndex.Item(k).split("-")(3)) Then
  89.                                 e.Row.Cells(2).Text = e.Row.Cells(2).Text
  90.                                 'if current cell2 (first date) text is equal to "Add New", then create Hyperlink
  91.                                 If Trim(e.Row.Cells(2).Text) = "Add New" Then
  92.                                     Dim link As HyperLink = New HyperLink()
  93.                                     link.Text = e.Row.Cells(2).Text
  94.                                     'Passing the date and name
  95.                                     link.NavigateUrl = "AddValues.aspx?DateValue=" & Trim(Header_Date1.Text) & "&NameValue=" & Trim(aryHyperLinkIndex.Item(k).split("-")(3))
  96.                                     e.Row.Cells(2).Controls.Add(link)
  97.                                     GoTo SecondCell
  98.                                 Else
  99.                                     Dim link As HyperLink = New HyperLink()
  100.                                     link.Text = e.Row.Cells(2).Text
  101.                                     'Passing index
  102.                                     link.NavigateUrl = "ViewUpdates.aspx?ViewUpdateValue=" & Trim(aryHyperLinkIndex.Item(k).Split("-")(2))
  103.                                     e.Row.Cells(2).Controls.Add(link)
  104.                                     GoTo SecondCell
  105.                                 End If
  106.                             End If
  107.                         End If
  108.                     End If
  109.                 Next
  110. SecondCell:
  111.  
  112.         End If
  113.     End Sub
  114.  
Jan 4 '11 #9

Post your reply

Sign in to post your reply or Sign up for a free account.