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
Sub Display_Table
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()
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
'Get distinct Date from database
aryDate.Add(myreader(0))
dcol = New DataColumn(Trim(myreader(0)))
Table_MAINPAGE.Columns.Add(dcol)
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
aryDate_Desc.Clear()
MySQL = "select ondate, description 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))
End While
For i As Integer = 0 To aryDate.Count - 1
gBool = False
For j As Integer = 0 To aryDate_Desc.Count - 1
If Trim(aryDate.Item(i)) = Trim(aryDate_Desc.Item(j).Split("-")(0)) Then
'Initialize the row data.
Row(Trim(aryDate.Item(i))) = Trim(aryDate_Desc.Item(j).Split("-")(1))
gBool = True
GoTo NextArrayValue
End If
Next
NextArrayValue:
If gBool <> True Then
Row(Trim(aryDate.Item(i))) = "Add New"
End If
Next
'Add the row to the datatable.
Table_MAINPAGE.Rows.Add(Row)
myreader.Close()
Next
'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
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.