Vb.net export data to excel - vb.net

I have a problem with the following code to export the data to excel with following button click Event:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
ExportToExcel()
End Sub
here is the code
Private Sub ExportToExcel()
Dim ExcelApp As Object, ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer
Try
Me.DgView.DefaultCellStyle.ForeColor = Color.Navy
Me.DgView.RowsDefaultCellStyle.BackColor = Color.AliceBlue
Me.DgView.GridColor = Color.Blue
Me.DgView.BorderStyle = BorderStyle.Fixed3D
Me.DgView.RowHeadersBorderStyle = BorderStyle.Fixed3D
DgView.AutoGenerateColumns = False
Dim ReferColumn As New DataGridViewTextBoxColumn()
ReferColumn.DataPropertyName = "Refer_no"
ReferColumn.HeaderText = "ካሴት መ/ቁ"
ReferColumn.Width = 80
Dim TitleColumn As New DataGridViewTextBoxColumn()
TitleColumn.DataPropertyName = "Title"
TitleColumn.HeaderText = "ካሴት ርእስ"
TitleColumn.Width = 150
Dim GnrNmColumn As New DataGridViewTextBoxColumn()
GnrNmColumn.DataPropertyName = "Genre_Name"
GnrNmColumn.HeaderText = "የካሴት ዓይነት"
GnrNmColumn.Width = 100
Dim GrpNmColumn As New DataGridViewTextBoxColumn()
GrpNmColumn.DataPropertyName = "Group_Name"
GrpNmColumn.HeaderText = "የምድብ ስም"
GrpNmColumn.Width = 100
Dim ActColumn As New DataGridViewTextBoxColumn()
ActColumn.DataPropertyName = "Actors"
ActColumn.HeaderText = "ሪፖርተር"
ActColumn.Width = 150
Dim DirecColumn As New DataGridViewTextBoxColumn()
DirecColumn.DataPropertyName = "Director"
DirecColumn.HeaderText = "ኃላፊ"
DirecColumn.Width = 150
Dim LangColumn As New DataGridViewTextBoxColumn()
LangColumn.DataPropertyName = "Language"
LangColumn.HeaderText = "ቋንቋ"
LangColumn.Width = 150
Dim releasedateColumn As New DataGridViewTextBoxColumn()
LangColumn.DataPropertyName = "release_date"
LangColumn.HeaderText = "የተቀረፀበተት ቀነን"
LangColumn.Width = 150
Dim StatusColumn As New DataGridViewTextBoxColumn()
StatusColumn.DataPropertyName = "status"
StatusColumn.HeaderText = "ሁኔታ"
StatusColumn.Width = 80
Dim synopsisColumn As New DataGridViewTextBoxColumn()
synopsisColumn.DataPropertyName = "synopsis"
synopsisColumn.HeaderText = "የተቀረፀበተት ጉዳይ"
synopsisColumn.Width = 180
DgView.Columns.Add(ReferColumn)
DgView.Columns.Add(TitleColumn)
DgView.Columns.Add(GnrNmColumn)
DgView.Columns.Add(GrpNmColumn)
DgView.Columns.Add(ActColumn)
DgView.Columns.Add(DirecColumn)
DgView.Columns.Add(LangColumn)
DgView.Columns.Add(releasedateColumn)
DgView.Columns.Add(StatusColumn)
DgView.Columns.Add(synopsisColumn)
Dim columns As String() = {"Refer_No", "Title", "genre_name", "group_name", "Actors", "Director", "Language", "release_date", "Status", "synopsis"}
DVDModule.FillListWithoutParam(DVDList, columns, "usp_SelectDVDList", _
GetType(Entity.DVD))
FormatGridWithBothTableAndColumnStyles()
DgView.DataSource = DVDList
'create object of excel
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)
With ExcelSheet
For Each column As DataGridViewColumn In DgView.Columns
.cells(1, column.Index + 1) = column.HeaderText
Next
For i = 1 To Me.DgView.RowCount
.cells(i + 1, 1) = Me.DgView.Rows(i - 1).Cells("Refer_no").Value
For j = 1 To DgView.Columns.Count - 1
.cells(i + 1, j + 1) = DgView.Rows(i - 1).Cells(j).Value
Next
Next
End With
ExcelApp.Visible = True
'
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing

I admit I didn't read all of your code. It's a mess and there's too much code for your question. It gives you some kind of error or what?
Anyway, if you just want to export a table showing some data, I suggest you to create a .csv file instead of using MS Excel. It's much more easier to code, faster during execution, and the output file is portable

Related

Speed up my function - Dynamic button creation vb.net

I have a function that creates button dynamically based on a database table, there are about 250 records inside. Everything is working properly but I'd like to make it faster but I don't know how.
Maybe it's due to the language I don't know.
Actually, on my development machine, it takes about 56 seconds to load. I'm not an experienced developper so maybe I'm missing something.
The problem is that on the client machine, that has only 4Go of RAM, the loading time is much bigger, 2.5 to 3.5 minutes.
Thans you for your help.
This is my code :
Public Sub LoadProducts()
Me.ProgressBar1.Value = 0
Me.ProgressBar1.Visible = True
Dim oSim As New SimFonction
Dim str As String = "SELECT * FROM produits ORDER BY ref;"
Dim oCmd As New MySqlCommand(str, oSim.ConnectDb)
Dim oData As New DataSet
Dim oAdapt As New MySqlDataAdapter With {
.SelectCommand = oCmd
}
oAdapt.Fill(oData)
oSim.conn.Close()
recCount = oData.Tables(0).Rows.Count
Dim btn(recCount) As Button
Dim x, y, j As Integer
oAdapt = Nothing
oCmd = Nothing
str = Nothing
Dim ref As String
x = 0
y = 0
Dim valeurIncr As Integer = 100 / recCount
valeurBar = 0
i = 0
For i = 0 To oData.Tables(0).Rows.Count - 1
If Me.ProgressBar1.Value >= 90 Then
Me.ProgressBar1.Value = 0
End If
If j = 5 Then
y += 90
j = 0
x = 0
End If
x += 90
btn(i) = New Button With {
.Parent = productPanel,
.Location = New Point(x, y),
.Height = 90,
.Width = 90,
.Font = New Font("Microsoft Sans Serif", 14, FontStyle.Bold),
.ForeColor = Color.Orange,
.UseVisualStyleBackColor = True,
.TextAlign = ContentAlignment.BottomCenter
}
ref = oData.Tables(0).Rows(i).Item("ref").ToString
If IsDBNull(oData.Tables(0).Rows(i).Item("photo")) Then
btn(i).Text = oData.Tables(0).Rows(i).Item("ref").ToString
btn(i).Tag = oData.Tables(0).Rows(i).Item("ref").ToString
btn(i).Name = "bt" & ref
Else
Dim photoP As New IO.MemoryStream(CType(oData.Tables(0).Rows(i).Item("photo"), Byte()))
btn(i).BackgroundImage = Image.FromStream(photoP)
btn(i).BackgroundImageLayout = ImageLayout.Stretch
btn(i).Tag = oData.Tables(0).Rows(i).Item("ref").ToString
btn(i).Text = oData.Tables(0).Rows(i).Item("ref").ToString
btn(i).Name = "bt" & ref
photoP = Nothing
End If
If oSim.CheckProduiEnStock(ref) = False Then
btn(i).Enabled = False
End If
AddHandler btn(i).Click, AddressOf ButtonClicked
j += 1
Me.ProgressBar1.Value += 1
btn(i) = Nothing
Next
Me.ProgressBar1.Visible = False
End Sub
You may use the following command:
At the beginning: Me.SuspendLayout() ,and
at the end: Me.ResumeLayout()

Hangman vb.net only first letter showing

Basically I generate a random word for example
"Tree" and when I press the T button it changes the label into a T but then when I choose R it doesnt show, can someone else see what i've done wrong?
here is my code
Sub GuessLetter(ByVal LetterGuess As String)
Dim strGuessedSoFar As String = Lbltempword.Text
Dim LengthOfSecretWord As Integer
LengthOfSecretWord = secret.Length - 1
tempWord = ""
Dim letterPosition As Integer
For letterPosition = 0 To LengthOfSecretWord
If secret.Substring(letterPosition, 1) = LetterGuess Then
tempWord = tempWord & LetterGuess
Else
tempWord = tempWord & Lbltempword.Text.Substring(letterPosition, 1)
End If
Next
Lbltempword.Text = tempWord
If Lbltempword.Text = secret Then 'YOU WIN
DisableButtons()
BtnStart.Enabled = True
MsgBox("YOU WIN")
End If
If Lbltempword.Text = strGuessedSoFar Then
NumWrong = NumWrong + 1
End If
DisplayHangman(NumWrong)
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
randomword()
MsgBox(secret)
EnableButtons()
BtnStart.Enabled = False
'Load up the temp word label with dashes
Secret_Word = secret
LoadLabelDisplay()
NumWrong = 0
DisplayHangman(NumWrong)
End Sub
Sub LoadLabelDisplay()
Lbltempword.Text = ""
Dim LengthOfSecretWord As Integer
LengthOfSecretWord = secret.Length - 1
Dim LetterPosition As Integer
For LetterPosition = 0 To LengthOfSecretWord
Lbltempword.Text = Lbltempword.Text & "-"
Next
End Sub
I also generate the random words by doing this.
Sub randomword()
Dim RAND(16)
Dim rng As New System.Random()
For i = 0 To 16
RAND(0) = "Tree"
RAND(1) = "Star"
RAND(2) = "Jesus"
RAND(3) = "Present"
RAND(4) = "advent"
RAND(5) = "Calender"
RAND(6) = "Jinglebell"
RAND(7) = "skint"
RAND(8) = "lapland"
RAND(9) = "Santa"
RAND(10) = "raindeer"
RAND(11) = "Cookies"
RAND(12) = "Milk"
RAND(13) = "nothing"
RAND(14) = "play"
RAND(15) = "sack"
Next
secret = RAND(rng.Next(RAND.Count()))
End Sub

How can I add multiple titles on a gridview

I'm working with a gridview on vb.net for webforms, I have to add titles to specific groups of columns in this table.. but I have not a clue of how to do it, heres an example of what i want:
"Student info", "Subject info" and "Partial Grades" ARE the titles I need to add.
Default Datagridview hasnt got an option as u wish.
You can try devexpress (It has got tones of properties in gridcontrol)
You can create your own control acting like your "TITLE LABELS"
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim DataGridView1 = New System.Windows.Forms.DataGridView()
Dim Column1 = New System.Windows.Forms.DataGridViewTextBoxColumn()
Dim Column2 = New System.Windows.Forms.DataGridViewTextBoxColumn()
Dim Column3 = New System.Windows.Forms.DataGridViewTextBoxColumn()
Dim TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
Dim Label1 = New System.Windows.Forms.Label()
Dim Label2 = New System.Windows.Forms.Label()
'DataGridView1
DataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.None
DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Column1, Column2, Column3})
DataGridView1.Location = New System.Drawing.Point(13, 78)
DataGridView1.Name = "DataGridView1"
DataGridView1.RowHeadersVisible = False
DataGridView1.Size = New System.Drawing.Size(397, 228)
DataGridView1.TabIndex = 0
DataGridView1.Name = "dgv"
AddHandler DataGridView1.ColumnWidthChanged, AddressOf DataGridView1_ColumnWidthChanged
'
'Column1
'
Column1.HeaderText = "Column1"
Column1.Name = "Column1"
'
'Column2
'
Column2.HeaderText = "Column2"
Column2.Name = "Column2"
'
'Column3
'
Column3.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill
Column3.HeaderText = "Column3"
Column3.Name = "Column3"
'
'TableLayoutPanel1
'
TableLayoutPanel1.ColumnCount = 2
TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 53.98936!))
TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 195.0!))
TableLayoutPanel1.Controls.Add(Label2, 1, 0)
TableLayoutPanel1.Controls.Add(Label1, 0, 0)
TableLayoutPanel1.Location = New System.Drawing.Point(12, 51)
TableLayoutPanel1.Name = "TableLayoutPanel1"
TableLayoutPanel1.RowCount = 1
TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
TableLayoutPanel1.Size = New System.Drawing.Size(398, 29)
TableLayoutPanel1.TabIndex = 1
TableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.[Single]
TableLayoutPanel1.Name = "title"
'
'Label1
'
Label1.Anchor = System.Windows.Forms.AnchorStyles.None
Label1.AutoSize = True
Label1.Location = New System.Drawing.Point(82, 8)
Label1.Name = "Label1"
Label1.Size = New System.Drawing.Size(39, 13)
Label1.TabIndex = 0
Label1.Text = "Label1"
'
'Label2
'
Label2.Anchor = System.Windows.Forms.AnchorStyles.None
Label2.AutoSize = True
Label2.Location = New System.Drawing.Point(281, 8)
Label2.Name = "Label2"
Label2.Size = New System.Drawing.Size(39, 13)
Label2.TabIndex = 1
Label2.Text = "Label2"
'
'Form1
'
Me.Controls.Add(TableLayoutPanel1)
Me.Controls.Add(DataGridView1)
End Sub
Private Sub DataGridView1_ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs)
Dim title As TableLayoutPanel = CType(Me.Controls("title"), TableLayoutPanel)
Dim dgv As DataGridView = CType(Me.Controls("dgv"), DataGridView)
title.ColumnStyles(0).Width = dgv.Columns(0).Width + dgv.Columns(1).Width
title.ColumnStyles(1).Width = dgv.Columns(2).Width
End Sub
End Class

Telerik winforms radgridview hierarchical data not showing correctly

Good day,
I am using telerik radgridview hierarchical data in vb.net winforms vs2013.
I am getting this problem, I see correctly the first list in the radgridview and in the first gridviewtemplate, but I do not see a thing in the second gridviewtemplate. I am out of ideas, I post the code here to see if anyone can help me.
Private Function loaddt0() As Data.DataSet
Dim dt As New Data.DataSet
dt.Tables.Add("person")
dt.Tables("person").Columns.Add("personID")
dt.Tables("person").Columns.Add("personName")
For i As Integer = 1 To 3
Dim rw As Data.DataRow
rw = dt.Tables("person").NewRow
rw.Item("personID") = i
rw.Item("personName") = "person" & " - " & i.ToString
dt.Tables("person").Rows.Add(rw)
Next
Return dt
End Function
Private Function loaddt1() As Data.DataSet
Dim dt As New Data.DataSet
dt.Tables.Add("project")
dt.Tables("project").Columns.Add("projectID")
dt.Tables("project").Columns.Add("projectName")
dt.Tables("project").Columns.Add("personID")
dt.Tables("project").Columns.Add("personprojectID")
Dim rw As Data.DataRow
rw = dt.Tables("project").NewRow
rw.Item("projectID") = 1
rw.Item("projectName") = "Proyect 1"
rw.Item("personID") = 1
rw.Item("personprojectID") = "1-1"
dt.Tables("project").Rows.Add(rw)
Dim rw1 As Data.DataRow
rw1 = dt.Tables("project").NewRow
rw1.Item("projectID") = 2
rw1.Item("projectName") = "Proyect 2"
rw1.Item("personID") = 1
rw1.Item("personprojectID") = "1-2"
dt.Tables("project").Rows.Add(rw1)
Dim rw2 As Data.DataRow
rw2 = dt.Tables("project").NewRow
rw2.Item("projectID") = 1
rw2.Item("projectName") = "Proyect 1"
rw2.Item("personID") = 2
rw2.Item("personprojectID") = "2-1"
dt.Tables("project").Rows.Add(rw2)
Dim rw3 As Data.DataRow
rw3 = dt.Tables("project").NewRow
rw3.Item("projectID") = 1
rw3.Item("projectName") = "Proyect 1"
rw3.Item("personID") = 3
rw3.Item("personprojectID") = "3-1"
dt.Tables("project").Rows.Add(rw3)
Return dt
End Function
Private Function loaddt2() As Data.DataSet
Dim dt As New Data.DataSet
dt.Tables.Add("task")
dt.Tables("task").Columns.Add("personprojectID")
dt.Tables("task").Columns.Add("taskID")
dt.Tables("task").Columns.Add("taskName")
dt.Tables("task").Columns.Add("personID")
dt.Tables("task").Columns.Add("personprojecttaskID")
Dim rw As Data.DataRow
rw = dt.Tables("task").NewRow
rw.Item("personprojectID") = "1-1"
rw.Item("taskID") = 3
rw.Item("taskName") = "Task 3"
rw.Item("personID") = 1
rw.Item("personprojecttaskID") = "1-1-3"
dt.Tables("task").Rows.Add(rw)
Dim rw1 As Data.DataRow
rw1 = dt.Tables("task").NewRow
rw1.Item("personprojectID") = "1-1"
rw1.Item("taskID") = 5
rw1.Item("taskName") = "Task 5"
rw1.Item("personID") = 1
rw1.Item("personprojecttaskID") = "1-1-5"
dt.Tables("task").Rows.Add(rw1)
Dim rw2 As Data.DataRow
rw2 = dt.Tables("task").NewRow
rw2.Item("personprojectID") = "1-1"
rw2.Item("taskID") = 8
rw2.Item("taskName") = "Task 8"
rw2.Item("personID") = 1
rw2.Item("personprojecttaskID") = "1-1-8"
dt.Tables("task").Rows.Add(rw2)
Dim rw3 As Data.DataRow
rw3 = dt.Tables("task").NewRow
rw3.Item("personprojectID") = "1-2"
rw3.Item("taskID") = 6
rw3.Item("taskName") = "Task 6"
rw3.Item("personID") = 1
rw3.Item("personprojecttaskID") = "1-2-6"
dt.Tables("task").Rows.Add(rw3)
Dim rw4 As Data.DataRow
rw4 = dt.Tables("task").NewRow
rw4.Item("personprojectID") = "2-1"
rw4.Item("taskID") = 1
rw4.Item("taskName") = "Task 1"
rw4.Item("personID") = 1
rw4.Item("personprojecttaskID") = "2-1-1"
dt.Tables("task").Rows.Add(rw4)
Dim rw5 As Data.DataRow
rw5 = dt.Tables("task").NewRow
rw5.Item("personprojectID") = "3-1"
rw5.Item("taskID") = 8
rw5.Item("taskName") = "Task 8"
rw5.Item("personID") = 3
rw5.Item("personprojecttaskID") = "3-1-8"
dt.Tables("task").Rows.Add(rw5)
Return dt
End Function
Private Sub load2()
Dim ldt As Data.DataSet
ldt = loaddt0()
rgvtareas.DataSource = ldt.Tables("person").DefaultView
ldt.Dispose()
Dim gridviewtemple As New Telerik.WinControls.UI.GridViewTemplate
Dim ldt2 As Data.DataSet
ldt2 = loaddt1()
gridviewtemple.DataSource = ldt2.Tables("project").DefaultView
ldt2.Dispose()
Dim ldt3 As Data.DataSet
ldt3 = loaddt2()
Dim gridviewtemple2 As New Telerik.WinControls.UI.GridViewTemplate
gridviewtemple2.DataSource = ldt3.Tables("task").DefaultView
ldt3.Dispose()
rgvtareas.MasterTemplate.Templates.Add(gridviewtemple)
Dim relation As New Telerik.WinControls.UI.GridViewRelation(rgvtareas.MasterTemplate)
relation.ChildTemplate = gridviewtemple
relation.RelationName = "PERSONS - PROJECTS"
relation.ParentColumnNames.Add("personID")
relation.ChildColumnNames.Add("personID")
rgvtareas.Relations.Add(relation)
gridviewtemple.AllowAddNewRow = False
gridviewtemple.AllowDragToGroup = False
gridviewtemple.AllowDeleteRow = False
gridviewtemple.AllowEditRow = False
gridviewtemple.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill
'Me.cargaridiomagrv3(gridviewtemple2)
'Me.dgdarformatogvt(gridviewtemple)
gridviewtemple.Templates.Add(gridviewtemple2)
Dim relation2 As New Telerik.WinControls.UI.GridViewRelation(gridviewtemple)
relation2.ChildTemplate = gridviewtemple2
relation2.RelationName = "PERSONPROJECTS - TASKS"
relation2.ParentColumnNames.Add("personprojectID")
relation2.ChildColumnNames.Add("personprojectID")
gridviewtemple2.AllowAddNewRow = False
gridviewtemple2.AllowDragToGroup = False
gridviewtemple2.AllowDeleteRow = False
gridviewtemple2.AllowEditRow = False
gridviewtemple2.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill
rgvtareas.Refresh()
End Sub
The image of what I get is:
You've missed adding the second GridViewRelation to the Relations collection of the control. Just add this line at the bottom:
rgvtareas.Relations.Add(relation2)

dynamic table and button creation in asp.net

I have created a dynamic table using for loop condition.In which it has a button while i click a specific button it should open a file.But in my coding it is opening a file button in the last row.
If Not IsPostBack Then
txtlogsdate.Text = FormatDate(Now)
End If
Try
trViewlogs.Visible = True
lbllogs.Visible = False
lbllogname.Visible = True
RT1.Visible = False
pb.Visible = False
Dim d1 As DateTime = txtlogsdate.Text
Dim dd As String = d1.ToString("dd")
Dim mm As String = d1.ToString("MM")
Dim yy As String = d1.ToString("yy")
Dim d2 As String = yy & "" & mm & "" & dd
Dim di As DirectoryInfo = New DirectoryInfo(Server.MapPath("~\logs"))
Dim files As FileInfo() = di.GetFiles("*.log")
Dim tab As New Table()
tab.CellPadding = 0
tab.CellSpacing = 0
tab.BorderStyle = BorderStyle.Double
tab.Attributes.Add("style", "margin-left: 0.5px; width: 800px;")
Dim row As New TableRow()
Dim headerCell1 As New TableHeaderCell()
headerCell1.Text = "Logs"
headerCell1.Attributes.Add("style", "margin-left: 0.5px; height: 20px;")
headerCell1.BackColor = System.Drawing.Color.CornflowerBlue
headerCell1.ForeColor = System.Drawing.Color.White
row.Controls.Add(headerCell1)
tab.Controls.Add(row)
Dim headerCell2 = New TableHeaderCell()
headerCell2.Attributes.Add("style", "margin-left: 0.5px; height: 20px;")
headerCell2.BackColor = System.Drawing.Color.CornflowerBlue
headerCell2.ForeColor = System.Drawing.Color.White
headerCell2.Text = "Download"
row.Controls.Add(headerCell2)
tab.Controls.Add(row)
For i As Integer = 0 To files.Length - 1
Dim a As String = files(i).ToString.Replace("Event-", "")
Dim c As String = a.Substring(0, 6)
Dim sw As String
If d2 = c Then
sw = My.Computer.FileSystem.ReadAllText(_
GetWebSitePhysicalRoot & "\logs\" & files(i).ToString)
lbllogname.Text = files(i).ToString
lbllogname.Visible = False
row = New TableRow()
If i Mod 2 = 0 Then
row.BackColor = System.Drawing.Color.White
Else
row.BackColor = System.Drawing.Color.AliceBlue
End If
Dim cell As New TableCell()
cell.Text = lbllogname.Text
'cell.Width = New Unit("1000px")
cell.HorizontalAlign = HorizontalAlign.Center
row.Controls.Add(cell)
Dim cell2 As New TableCell()
Dim bt As New Button
bt.BorderStyle = BorderStyle.Solid
bt.Text = files(i).ToString
AddHandler bt.Click, AddressOf bt_Click
cell2.HorizontalAlign = HorizontalAlign.Center
cell2.Controls.Add(bt)
row.Controls.Add(cell2)
tab.Controls.Add(row)
Panel1.Controls.Add(tab)
End If
Next i
If lbllogname.Text = "" Then
lbllogname.Text = "No Logs to Display !"
End If
Session("pageurl") = ""
Session("pagecount") = ""
ib.SetInfo("Reports > View Logs", Infobar.InfoTypes.Caption)
Dim mi As Integer = GetQueryStringToInt("menuindex", 1)
If Not IsPostBack Then
leftmenu1.AddItem("View Logs", _
GetWebSiteUrlRoot & "/staff_rpt.aspx?rpt=logs&page=1&menuindex=" & mi)
End If
Catch ex As Exception
WriteLog(LogWriter.EventType.eError, ex.StackTrace.ToString)
End Try
Protected Sub bt_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs
Dim a As String
a = lbllogname.Text
Response.ContentType = "text/plain"
Response.AppendHeader("Content-Disposition", "attachment; filename=" & a)
Response.TransmitFile(Server.MapPath("~/logs/" & a))
Response.End()
End Sub
To use the dynamic table structure you have built in your code, then you need to uniquely name each button in each row; otherwise the button click handler (bt_Click) cannot figure out the correct row to open the file in, because they are all called the same and will use the last one.
Since you want a table structure, then I suggest you use the GridView server control, as it will provide similar output, but provide the ability to use templating to name the controls of each row the same, but allow for you to differentiate individual rows when a click event happens.