Adding rows to a DataGridView control causes crash but only on second attempt - vb.net

I have a DataGridView (dgvNew) which is populated by a JSON file which is located by a FileSystemWatcher, data is added row by row after being read. It works fine on first file. But if i trigger a new file by copying and pasting the same JSON file it adds the rows again row by row as id expect, but then the whole form crashes with no error.
I've tried TRY..CATCH with WHILE loops for opened the files which works in terms of openning them and adding rows, i just don't understand why it crashes. The code continues to step through regardless even though the form is frozen ? is it Thread related ?
Public Sub subParseJSONs(strFilePath As String, strDesiredField As String)
Dim json As String
Dim strMachine As String
Dim read As New Newtonsoft.Json.Linq.JObject
Dim booErrorJSNOArrRead As Boolean
Dim i As Integer
Dim dgvIndex As Integer
Dim booOpened As Boolean
Dim k As Integer, j As Integer
booOpened = False
k = 1
j = 1
json = Nothing
While json Is Nothing
Try
j = j + 1
If j = 10 Then
MessageBox.Show("J integer reached 10")
Exit While
Exit Try
End If
json = Replace(Replace(System.IO.File.ReadAllText(strFilePath), vbLf, ""), vbTab, "")
read = Newtonsoft.Json.Linq.JObject.Parse(json)
Catch ex As IOException
'MessageBox.Show(ex.Message)
Threading.Thread.Sleep(300)
'GoTo EndOfSUb
Catch ex As Exception
'MessageBox.Show(ex.Message)
Threading.Thread.Sleep(300)
'GoTo EndOfSUb
Finally
booOpened = True
End Try
End While
booErrorJSNOArrRead = False
i = 0
dgvNew.ColumnCount = 6
dgvNew.Columns(0).Name = "TempID"
dgvNew.Columns(1).Name = "DriverName"
dgvNew.Columns(2).Name = "Seat"
dgvNew.Columns(3).Name = "RaceTime"
dgvNew.Columns(4).Name = "ResultTime"
dgvNew.Columns(5).Name = "CarDriven"
dgvNew.RefreshEdit()
dgvNew.Refresh()
Do Until i = read.Item("Result").Count
If Not read.Item("Result")(i)("DriverName") = "" Then
Dim milliseconds As Double = Convert.ToDouble(read.Item("Result")(i)("TotalTime"))
Dim ts As TimeSpan = TimeSpan.FromMilliseconds(milliseconds)
Dim strMMSSmmm As String = ts.Minutes.ToString & ":" & ts.Seconds.ToString & "." & ts.Milliseconds.ToString
Dim row As String() = New String() {i + 1,
read.Item("Result")(i)("DriverName"),
read.Item("Result")(i)("DriverName"),
strMMSSmmm,
DateTime.Now, read.Item("Result")(i)("CarModel")}
dgvNew.Rows.Add(row)
End If
i = i + 1
Loop
read = Nothing
End Sub
I'm expecting new rows to be added to the bottom of dgvNew, which they are, but then it crashes ?

Related

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in vb.net CSV program

This program is simply reading from a CSV and putting that into a DatagridView table. I am not sure what is causing the error shown below.
"An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Minecraft Server Program.exe"
This error points to the line "newrow("Days_remaining") = columns(1)
Also, the CSV is formatted as such:
xX_EpicGamer_Xx,2,2/05/2021,9,6,Player was mean to others and stole lots of diamonds
pewdiepie,3,2/05/2021,4,2,Player swore
Thank you for any help
Public Sub LoadCSVFile()
'If the Input file cannot be found, then give error message And exit this Subroutine.
If Not File.Exists(BansCSVFile) Then
MsgBox("Input CSV File of Item Prices not found: " & BansCSVFile)
Exit Sub
End If
'Set up streamreader and variables for reading through file
Dim srdCSV As New IO.StreamReader("bans.csv", System.Text.Encoding.Default)
Dim sline As String = ""
Do
'Read through each line, separating with the comma and putting each entry into its respective column
sline = srdCSV.ReadLine
If sline Is Nothing Then Exit Do
Dim columns() As String = sline.Split(",")
Dim newrow As DataRow = datatable1.NewRow
newrow("Player") = columns(0)
newrow("Days_remaining") = columns(1)
newrow("Date_started") = columns(2)
newrow("Timespan") = columns(3)
newrow("Rule_broken") = columns(4)
newrow("Extra_info") = columns(5)
datatable1.Rows.Add(newrow)
Loop
srdCSV.Close()
dgvBans.DataSource = datatable1
Me.Text = datatable1.Rows.Count & "rows"
End Sub
Edit:
This new code fixed the issue.
Public Sub LoadCSVFile()
'If the Input file cannot be found, then give error message And exit this Subroutine.
If Not File.Exists(BansCSVFile) Then
MsgBox("Input CSV File of Item Prices not found: " & BansCSVFile)
Exit Sub
End If
'Set up streamreader and variables for reading through file
srdCSV = New IO.StreamReader("bans.csv")
Dim totalrec As Integer = 0
Try
Do Until srdCSV.Peek = -1
'Read through each line, separating with the comma and putting each entry into its respective column
strInputFileLine = srdCSV.ReadLine()
'Split CSV lines where commas are
Dim RecordLineColumns() As String = strInputFileLine.Split(",")
'Create a new row in data table and place data into it from array
Dim newrow As DataRow = datatable1.NewRow
newrow("Player") = RecordLineColumns(0)
newrow("Days_remaining") = RecordLineColumns(1)
newrow("Date_started") = RecordLineColumns(2)
newrow("Timespan") = RecordLineColumns(3)
newrow("Rule_broken") = RecordLineColumns(4)
newrow("Extra_info") = RecordLineColumns(5)
datatable1.Rows.Add(newrow)
totalrec = totalrec + 1
Loop
Catch ex As Exception
MsgBox("Error reading file")
Exit Sub
End Try
srdCSV.Close()
'Me.Text = datatable1.Rows.Count & "rows"
MsgBox("Total input records = " & totalrec)
End Sub
using the Try/Catch syntax
After checking which column has a problem, try verifying the csv file.
I am guessing that the Extra_Info is not necessarily present for every record. I am checking for a length less 5 and exiting but if it has at least 5 we continue but only fill in the last field if the length is 6.
Public Sub LoadCSVFile(BansCSVFile As String)
Dim datatable1 As New DataTable
With datatable1.Columns
.Add("Player")
.Add("Days_remaing")
.Add("Date_started")
.Add("Timespan")
.Add("Rule_broken")
.Add("Extra_Info")
End With
If Not File.Exists(BansCSVFile) Then
MsgBox("Input CSV File of Item Prices not found: " & BansCSVFile)
Exit Sub
End If
Dim lines = File.ReadAllLines(BansCSVFile)
For Each sline In lines
Dim columns() As String = sline.Split(","c)
If columns.Length < 5 Then
MessageBox.Show($"Incomplete data on line {sline}")
Return
End If
Dim newrow As DataRow = datatable1.NewRow
newrow("Player") = columns(0)
newrow("Days_remaining") = columns(1)
newrow("Date_started") = columns(2)
newrow("Timespan") = columns(3)
newrow("Rule_broken") = columns(4)
If columns.Length = 6 Then
newrow("Extra_info") = columns(5)
End If
datatable1.Rows.Add(newrow)
Next
dgvBans.DataSource = datatable1
Me.Text = datatable1.Rows.Count & "rows"
End Sub

Get YouTube channel data using Google YouTube Data API in VB.NET

I'd like to get channel data using the YouTube Data API in a VB.NET application. I can't find any kind of documentation, everything is in .NET and Google documentation is way too cryptic for me.
I used to get these data using URL request, but I'd like to do it more... programmatically!
I added Google.Apis.YouTube.v3 Nuget, but can't figure how to set credential and retrieve data.
There is a VB .NET ResumableUpload example in this GITHUB repository that contains some code that should help you get started.
This is some sample code that retrieves all of the videos in the "Uploads" PlayList for the logged on channel.
Imports Google.Apis.YouTube.v3
Imports Google.Apis.YouTube.v3.Data
...
...
Dim strUploadsListId As String = ""
Try
bOK = False
Dim objChannelListRequest As ChannelsResource.ListRequest = objYouTubeService.Channels.List("contentDetails")
objChannelListRequest.Mine = True
Dim objChannelListResponse As ChannelListResponse = objChannelListRequest.Execute
Dim objChannel As Channel
For Each objChannel In objChannelListResponse.Items
strUploadsListId = objChannel.ContentDetails.RelatedPlaylists.Uploads ' The Uploads PlayList
Debug.WriteLine("PlayList ID=" & strUploadsListId)
Next
bOK = True
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "ChannelListRequest")
End Try
If bOK Then
Dim objNextPageToken As String = ""
While Not objNextPageToken Is Nothing
Dim objPlayListItemRequest As PlaylistItemsResource.ListRequest = objYouTubeService.PlaylistItems.List("contentDetails")
Dim objPlayListItemsListResponse As PlaylistItemListResponse = Nothing
objPlayListItemRequest.PlaylistId = strUploadsListId
objPlayListItemRequest.MaxResults = 50
objPlayListItemRequest.PageToken = objNextPageToken
Try
bOK = False
objPLayListItemsListResponse = objPlayListItemRequest.Execute
bOK = True
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "PlayListRequest")
End Try
If bOK Then
Dim objPlayListItem As PlaylistItem
Dim strVideoIds As New StringBuilder("") With {.Capacity = objPLayListItemsListResponse.Items.Count * 16}
For Each objPlayListItem In objPlayListItemsListResponse.Items
strVideoIds.Append(objPlayListItem.ContentDetails.VideoId)
strVideoIds.Append(",")
Next
strVideoIds.Remove(strVideoIds.Length - 1, 1) ' Remove Last Character (Extra comma)
Dim objListRequest As VideosResource.ListRequest
Dim objVideoListResponse As VideoListResponse = Nothing
Try
bOK = False
objListRequest = New VideosResource.ListRequest(objYouTubeService, "id,snippet,recordingDetails,status,contentDetails") With {.Id = strVideoIds.ToString}
Debug.WriteLine("IDs to retrieve: " & strVideoIds.ToString)
objVideoListResponse = objListRequest.Execute
bOK = True
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "ListRequest")
End Try
If bOK Then
For Each objVideo As Video In objVideoListResponse.Items
Dim TheTitle as string = objVideo.Snippet.Title
Dim Embeddable as boolean = objVideo.Status.Embeddable
Dim dtRecorded as date - Nothing
If (Not objVideo.RecordingDetails Is Nothing) AndAlso (Not objVideo.RecordingDetails.RecordingDate Is Nothing) Then
dtRecorded = CDate(objVideo.RecordingDetails.RecordingDate)
End If
Dim Duration As Date = GetDuration(objVideo.ContentDetails.Duration)
Dim Category As string = objVideo.Snippet.CategoryId
Dim PrivacyStatus As string = objVideo.Status.PrivacyStatus
Dim Description as string = objVideo.Snippet.Description AndAlso
'
'
Next
End If
End If
objNextPageToken = objPlayListItemsListResponse.NextPageToken
End While
End If
'_______________________________________________________
Friend Function GetDuration(ByVal Duration As String) As Date ' Only an elapsed time value
' Format returned from YouTube: PT#H#M#S or PT#M#S or PT#S
GetDuration = EMPTYDATE
If Duration IsNot Nothing Then
If Duration.StartsWith("PT") Then
Dim x As Integer = 2
Dim y As Integer = x
Dim Hours As Integer = 0
Dim Minutes As Integer = 0
Dim Seconds As Integer = 0
Do
While y < Duration.Length AndAlso IsNumeric(Duration.Substring(y, 1))
y += 1
End While
If y < Duration.Length Then
Select Case Duration.Substring(y, 1)
Case "H"
Hours = CInt(Duration.Substring(x, y - x))
Case "M"
Minutes = CInt(Duration.Substring(x, y - x))
Case "S"
Seconds = CInt(Duration.Substring(x, y - x))
End Select
End If
x = y + 1
y = x
Loop Until x >= Duration.Length
GetDuration = CDate("01/01/1900 " & Format(Hours, "00") & ":" & Format(Minutes, "00") & ":" & Format(Seconds, "00"))
End If
End If
End Function
I did it, thanks to Mike Meinz and th Visual Studio debugging tools
Here the code to get some channels (not necessary yours) data using YouTube Data API in a VB.NET:
Dim youtube_api_key As String = "Your_Key"
Dim youtube_api_application_name As String = "Your_Project_Name_In_the_Google_Developper_Console"
Dim youtube_initialiser As New Google.Apis.Services.BaseClientService.Initializer()
youtube_initialiser.ApiKey = youtube_api_key
youtube_initialiser.ApplicationName = youtube_api_application_name
Dim youtube_service As Google.Apis.YouTube.v3.YouTubeService = New YouTubeService(youtube_initialiser)
Dim objChannelListRequest As ChannelsResource.ListRequest = youtube_service.Channels.List("id,snippet,statistics")
objChannelListRequest.Id = youtube_channel
Dim objChannelListResponse As ChannelListResponse = objChannelListRequest.Execute()
Debug.Print(objChannelListResponse.Items(0).Snippet.Description)
Debug.Print(objChannelListResponse.Items(0).Statistics.SubscriberCount)
Debug.Print(objChannelListResponse.Items(0).Statistics.VideoCount)
Debug.Print(objChannelListResponse.Items(0).Statistics.ViewCount)

system.io.ioexception the process cannot access because it is being used by another process

i am getting this problem in some systems, some systems working properly, here my code is,
Dim fileName As String = "FaultTypesByMonth.csv"
Using writer As IO.StreamWriter = New IO.StreamWriter(fileName, True, System.Text.Encoding.Default) '------------ rao new ----
Dim Str As String
Dim i As Integer
Dim j As Integer
Dim headertext1(rsTerms.Columns.Count) As String
Dim k As Integer = 0
Dim arrcols As String = Nothing
For Each column As DataColumn In TempTab.Columns
arrcols += column.ColumnName.ToString() + ","c
k += 1
Next
writer.WriteLine(arrcols)
For i = 0 To (TempTab.Rows.Count - 1)
For j = 0 To (TempTab.Columns.Count - 1)
If j = (TempTab.Columns.Count - 1) Then
Str = (TempTab.Rows(i)(j).ToString)
Else
Str = (TempTab.Rows(i)(j).ToString & ",")
End If
writer.Write(Str)
Next
writer.WriteLine()
Next
writer.Close()
writer.Dispose()
End Using
Dim FileToDelete As String = Nothing
Dim sd As New SaveFileDialog
sd.Filter = "CSV Files (*.csv)|*.csv"
sd.FileName = "FaultTypesByMonth"
If sd.ShowDialog = Windows.Forms.DialogResult.OK Then
FileCopy(fileName, sd.FileName)
MsgBox(" File Saved in selected path")
FileToDelete = fileName
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
End If
FileToDelete = fileName
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
when i am trying to save this file in desired path, then i am getting this error.
if save in shared folder i am not getting this error
system.io.ioexception the process cannot access because it is being used by another process...
what i am doing wrong,Help me

Finding Missing numbers in a given range

So i have a problem with my codings and was wondering if anyone can help me.
Basically i'm using VB.NET and MSSQL to make a program on finding missing numbers in between a given range set by the user. The program will read from the table and give the output on a textbox. And the above codes are so far what i can come up with. But the problem is, i get wrong output and not what i want. Here's an image of the output.
Function FindingMissingNumber() As String
Dim intX As Integer = Nothing
Dim intY As Integer = Nothing
Dim strSting As String = Nothing
Dim strSqlQUery As String = Nothing
Dim cmdSqlCommand As Data.SqlClient.SqlCommand = Nothing
Dim rdrDataReader As Data.SqlClient.SqlDataReader = Nothing
'------------------------------------------------------------------------------------------------------------------------
'-> Process
'------------------------------------------------------------------------------------------------------------------------
strSqlQUery = "Select ExReportPolicyNo From DBReport Order by ExReportPolicyNo"
Dim msSqlConnection As New Data.SqlClient.SqlConnection()
'NOTE - You may need to CHECK your connection string!!! in the line below
msSqlConnection.ConnectionString = "Data Source=SISBSQL\SISBSQL;Initial Catalog=ExceptionReport;User ID=sa;Password=123;"
cmdSqlCommand = New Data.SqlClient.SqlCommand(strSqlQUery, msSqlConnection)
If cmdSqlCommand.Connection.State = Data.ConnectionState.Closed Then cmdSqlCommand.Connection.Open()
rdrDataReader = cmdSqlCommand.ExecuteReader()
If rdrDataReader.HasRows Then
Do While rdrDataReader.Read()
intX = txtRangeLeft.Text
intY = txtRangeRight.Text
'intY = rdrDataReader.GetValue(rdrDataReader.GetOrdinal("ExReportPolicyNo"))
Do While intX <> intY
intX = intX + 1
If intX <> intY Then
strSting = strSting & intX & ", " 'if it is not, then record the non sequential number into the string
Else
Exit Do
End If
Loop
Loop
End If
If cmdSqlCommand.Connection.State = Data.ConnectionState.Open Then cmdSqlCommand.Connection.Close()
'return string
Return strSting
'tidy up
intX = Nothing
intY = Nothing
strSting = Nothing
strSqlQUery = Nothing
cmdSqlCommand = Nothing
rdrDataReader = Nothing
End Function
As you can see the program loops it multiple times, and give out the wrong output. The output should read only "286118, 286120, 286121". Question is where did i went wrong?
Try this (using linq)
Change query to return rows between start and end value
Select distinct ExReportPolicyNo From DBReport
Where ExReportPolicyNo between #start and #end
Order by ExReportPolicyNo
Create List from your query:
Dim originalList as List(Of Integer)
If rdrDataReader.HasRows Then
Do While rdrDataReader.Read()
originalList.Add(rdrDataReader.GetInt(0))
Loop
End If
Create List of range from your start and end number
//Dim rangeList = Enumerable.Range(286117, 286121 - 286117 + 1).ToList()
Dim starti = Int32.Parse(txtRangeLeft.Text)
Dim endi = Int32.Parse(txtRangeRight.Text)
Dim rangeList = Enumerable.Range(starti, endi - starti + 1).ToList()
Find all missing numbers
Dim missingList = originalList.Except(rangelist)
Create CSV string from list above
strString = String.Join(",", missingList.Select(x => x.ToString()).ToArray())

How to export datagridview to excel using vb.net?

I have a datagridview in vb.net that is filled up from the database. I've researched and I found out that there is no built in support to print directly from datagridview. I don't want to use crystal report because I'm not familiar with it.
I'm planning to export it to excel to enable me to generate report from the datagridview.
Can you provide me ways to do this?
Code below creates Excel File and saves it in D: drive
It uses Microsoft office 2007
FIRST ADD REFERRANCE (Microsoft office 12.0 object library ) to your project
Then Add code given bellow to the Export button click event-
Private Sub Export_Button_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles VIEW_Button.Click
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
For k As Integer = 1 To DataGridView1.Columns.Count
xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
Next
Next
Next
xlWorkSheet.SaveAs("D:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("You can find the file D:\vbexcel.xlsx")
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
Excel Method
This method is different than many you will see. Others use a loop to write each cell and write the cells with text data type.
This method creates an object array from a DataTable or DataGridView and then writes the array to Excel. This means I can write to Excel without a loop and retain data types.
I extracted this from my library and I think I changed it enough to work with this code only, but more minor tweaking might be necessary. If you get errors just let me know and I'll correct them for you. Normally, I create an instance of my class and call these methods. If you would like to use my library then use this link to download it and if you need help just let me know.
https://zomp.co/Files.aspx?ID=zExcel
After copying the code to your solution you will use it like this.
In your button code add this and change the names to your controls.
WriteDataGrid("Sheet1", grid)
To open your file after exporting use this line
System.Diagnostics.Process.Start("The location and filename of your file")
In the WriteArray method you'll want to change the line that saves the workbook to where you want to save it. Probably makes sense to add this as a parameter.
wb.SaveAs("C:\MyWorkbook.xlsx")
Public Function WriteArray(Sheet As String, ByRef ObjectArray As Object(,)) As String
Try
Dim xl As Excel.Application = New Excel.Application
Dim wb As Excel.Workbook = xl.Workbooks.Add()
Dim ws As Excel.Worksheet = wb.Worksheets.Add()
ws.Name = Sheet
Dim range As Excel.Range = ws.Range("A1").Resize(ObjectArray.GetLength(0), ObjectArray.GetLength(1))
range.Value = ObjectArray
range = ws.Range("A1").Resize(1, ObjectArray.GetLength(1) - 1)
range.Interior.Color = RGB(0, 70, 132) 'Con-way Blue
range.Font.Color = RGB(Drawing.Color.White.R, Drawing.Color.White.G, Drawing.Color.White.B)
range.Font.Bold = True
range.WrapText = True
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
range.Application.ActiveWindow.SplitColumn = 0
range.Application.ActiveWindow.SplitRow = 1
range.Application.ActiveWindow.FreezePanes = True
wb.SaveAs("C:\MyWorkbook.xlsx")
wb.CLose()
xl.Quit()
xl = Nothing
wb = Nothing
ws = Nothing
range = Nothing
ReleaseComObject(xl)
ReleaseComObject(wb)
ReleaseComObject(ws)
ReleaseComObject(range)
Return ""
Catch ex As Exception
Return "WriteArray()" & Environment.NewLine & Environment.NewLine & ex.Message
End Try
End Function
Public Function WriteDataGrid(SheetName As String, ByRef dt As DataGridView) As String
Try
Dim l(dt.Rows.Count + 1, dt.Columns.Count) As Object
For c As Integer = 0 To dt.Columns.Count - 1
l(0, c) = dt.Columns(c).HeaderText
Next
For r As Integer = 1 To dt.Rows.Count
For c As Integer = 0 To dt.Columns.Count - 1
l(r, c) = dt.Rows(r - 1).Cells(c)
Next
Next
Dim errors As String = WriteArray(SheetName, l)
If errors <> "" Then
Return errors
End If
Return ""
Catch ex As Exception
Return "WriteDataGrid()" & Environment.NewLine & Environment.NewLine & ex.Message
End Try
End Function
Public Function WriteDataTable(SheetName As String, ByRef dt As DataTable) As String
Try
Dim l(dt.Rows.Count + 1, dt.Columns.Count) As Object
For c As Integer = 0 To dt.Columns.Count - 1
l(0, c) = dt.Columns(c).ColumnName
Next
For r As Integer = 1 To dt.Rows.Count
For c As Integer = 0 To dt.Columns.Count - 1
l(r, c) = dt.Rows(r - 1).Item(c)
Next
Next
Dim errors As String = WriteArray(SheetName, l)
If errors <> "" Then
Return errors
End If
Return ""
Catch ex As Exception
Return "WriteDataTable()" & Environment.NewLine & Environment.NewLine & ex.Message
End Try
End Function
I actually don't use this method in my Database program because it's a slow method when you have a lot of rows/columns. I instead create a CSV from the DataGridView. Writing to Excel with Excel Automation is only useful if you need to format the data and cells otherwise you should use CSV. You can use the code after the image for CSV export.
CSV Method
Private Sub DataGridToCSV(ByRef dt As DataGridView, Qualifier As String)
Dim TempDirectory As String = "A temp Directory"
System.IO.Directory.CreateDirectory(TempDirectory)
Dim oWrite As System.IO.StreamWriter
Dim file As String = System.IO.Path.GetRandomFileName & ".csv"
oWrite = IO.File.CreateText(TempDirectory & "\" & file)
Dim CSV As StringBuilder = New StringBuilder()
Dim i As Integer = 1
Dim CSVHeader As StringBuilder = New StringBuilder()
For Each c As DataGridViewColumn In dt.Columns
If i = 1 Then
CSVHeader.Append(Qualifier & c.HeaderText.ToString() & Qualifier)
Else
CSVHeader.Append("," & Qualifier & c.HeaderText.ToString() & Qualifier)
End If
i += 1
Next
'CSV.AppendLine(CSVHeader.ToString())
oWrite.WriteLine(CSVHeader.ToString())
oWrite.Flush()
For r As Integer = 0 To dt.Rows.Count - 1
Dim CSVLine As StringBuilder = New StringBuilder()
Dim s As String = ""
For c As Integer = 0 To dt.Columns.Count - 1
If c = 0 Then
'CSVLine.Append(Qualifier & gridResults.Rows(r).Cells(c).Value.ToString() & Qualifier)
s = s & Qualifier & gridResults.Rows(r).Cells(c).Value.ToString() & Qualifier
Else
'CSVLine.Append("," & Qualifier & gridResults.Rows(r).Cells(c).Value.ToString() & Qualifier)
s = s & "," & Qualifier & gridResults.Rows(r).Cells(c).Value.ToString() & Qualifier
End If
Next
oWrite.WriteLine(s)
oWrite.Flush()
'CSV.AppendLine(CSVLine.ToString())
'CSVLine.Clear()
Next
'oWrite.Write(CSV.ToString())
oWrite.Close()
oWrite = Nothing
System.Diagnostics.Process.Start(TempDirectory & "\" & file)
GC.Collect()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DATAGRIDVIEW_TO_EXCEL((DataGridView1)) ' PARAMETER: YOUR DATAGRIDVIEW
End Sub
Private Sub DATAGRIDVIEW_TO_EXCEL(ByVal DGV As DataGridView)
Try
Dim DTB = New DataTable, RWS As Integer, CLS As Integer
For CLS = 0 To DGV.ColumnCount - 1 ' COLUMNS OF DTB
DTB.Columns.Add(DGV.Columns(CLS).Name.ToString)
Next
Dim DRW As DataRow
For RWS = 0 To DGV.Rows.Count - 1 ' FILL DTB WITH DATAGRIDVIEW
DRW = DTB.NewRow
For CLS = 0 To DGV.ColumnCount - 1
Try
DRW(DTB.Columns(CLS).ColumnName.ToString) = DGV.Rows(RWS).Cells(CLS).Value.ToString
Catch ex As Exception
End Try
Next
DTB.Rows.Add(DRW)
Next
DTB.AcceptChanges()
Dim DST As New DataSet
DST.Tables.Add(DTB)
Dim FLE As String = "" ' PATH AND FILE NAME WHERE THE XML WIL BE CREATED (EXEMPLE: C:\REPS\XML.xml)
DTB.WriteXml(FLE)
Dim EXL As String = "" ' PATH OF/ EXCEL.EXE IN YOUR MICROSOFT OFFICE
Shell(Chr(34) & EXL & Chr(34) & " " & Chr(34) & FLE & Chr(34), vbNormalFocus) ' OPEN XML WITH EXCEL
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Regarding your need to 'print directly from datagridview', check out this article on CodeProject:
The DataGridViewPrinter Class
There are a number of similar articles but I've had luck with the one I linked.
The following code works fine for me :)
Protected Sub ExportToExcel(sender As Object, e As EventArgs) Handles ExportExcel.Click
Try
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=ExportEthias.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Using sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GvActifs.RenderControl(hw)
'Le format de base est le texte pour éviter les problèmes d'arrondis des nombres
Dim style As String = "<style> .textmode { } </style>"
Response.Write(Style)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
End Using
Catch ex As Exception
lblMessage.Text = "Erreur export Excel : " & ex.Message
End Try
End Sub
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
' Verifies that the control is rendered
End Sub
Hopes this help you.
Dim rowNo1 As Short
Dim numrow As Short
Dim colNo1 As Short
Dim colNo2 As Short
rowNo1 = 1
colNo1 = 1
colNo2 = 1
numrow = 1
ObjEXCEL = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
objEXCELBook = CType(ObjEXCEL.Workbooks.Add, Microsoft.Office.Interop.Excel.Workbook)
objEXCELSheet = CType(objEXCELBook.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet)
ObjEXCEL.Visible = True
For numCounter = 0 To grdName.Columns.Count - 1
' MsgBox(grdName.Columns(numCounter).HeaderText())
If grdName.Columns(numCounter).Width > 0 Then
ObjEXCEL.Cells(1, numCounter + 1) = grdName.Columns(numCounter).HeaderText()
End If
' ObjEXCEL.Cells(1, numCounter + 1) = grdName.Columns.GetFirstColumn(DataGridViewElementStates.Displayed)
Next numCounter
ObjEXCEL.Range("A:A").ColumnWidth = 10
ObjEXCEL.Range("B:B").ColumnWidth = 25
ObjEXCEL.Range("C:C").ColumnWidth = 20
ObjEXCEL.Range("D:D").ColumnWidth = 20
ObjEXCEL.Range("E:E").ColumnWidth = 20
ObjEXCEL.Range("F:F").ColumnWidth = 25
For rowNo1 = 0 To grdName.RowCount - 1
For colNo1 = 0 To grdName.ColumnCount - 1
If grdName.Columns(colNo1).Width > 0 Then
If Trim(grdName.Item(colNo1, rowNo1).Value) <> "" Then
'If IsDate(grdName.Item(colNo1, rowNo1).Value) = True Then
' ObjEXCEL.Cells(numrow + 1, colNo2) = Format(CDate(grdName.Item(colNo1, rowNo1).Value), "dd/MMM/yyyy")
'Else
ObjEXCEL.Cells(numrow + 1, colNo2) = grdName.Item(colNo1, rowNo1).Value
'End If
End If
If colNo2 >= grdName.ColumnCount Then
colNo2 = 1
Else
colNo2 = colNo2 + 1
End If
End If
Next colNo1
numrow = numrow + 1
Next rowNo1
In design mode: Set DataGridView1 ClipboardCopyMode properties to EnableAlwaysIncludeHeaderText
or on the program code
DataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
In the run time select all cells content (Ctrl+A) and copy (Ctrl+C) and paste to the Excel Program.
Let the Excel do the rest
Sorry for the inconvenient, I have been searching the method to print data directly from the datagridvew (create report from vb.net VB2012) and have not found the satisfaction result.
Above code just my though, wondering if my applications user can rely on above simple step it will be nice and I could go ahead to next step on my program progress.
A simple way of generating a printable report from a Datagridview is to place the datagridview on a Panel object. It is possible to draw a bitmap of the panel.
Here's how I do it.
'create the bitmap with the dimentions of the Panel
Dim bmp As New Bitmap(Panel1.Width, Panel1.Height)
'draw the Panel to the bitmap "bmp"
Panel1.DrawToBitmap(bmp, Panel1.ClientRectangle)
I create a multi page tiff by "breaking my datagridview items into pages.
this is how i detect the start of a new page:
'i add the rows to my datagrid one at a time and then check if the scrollbar is active.
'if the scrollbar is active i save the row to a variable and then i remove it from the
'datagridview and roll back my counter integer by one(thus the next run will include this
'row.
Private Function VScrollBarVisible() As Boolean
Dim ctrl As New Control
For Each ctrl In DataGridView_Results.Controls
If ctrl.GetType() Is GetType(VScrollBar) Then
If ctrl.Visible = True Then
Return True
Else
Return False
End If
End If
Next
Return Nothing
End Function
I hope this helps
another easy way and more flexible , after loading data into Datagrid
Private Sub Button_Export_Click(sender As Object, e As EventArgs) Handles Button_Export.Click
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("c:\1\Myfile.csv", True)
If DataGridView1.Rows.Count = 0 Then GoTo loopend
' collect the header's names
Dim Headerline As String
For k = 0 To DataGridView1.Columns.Count - 1
If k = DataGridView1.Columns.Count - 1 Then ' last column dont put , separate
Headerline = Headerline & DataGridView1.Columns(k).HeaderText
Else
Headerline = Headerline & DataGridView1.Columns(k).HeaderText & ","
End If
Next
file.WriteLine(Headerline) ' this will write header names at the first line
' collect the data
For i = 0 To DataGridView1.Rows.Count - 1
Dim DataRow As String
For k = 0 To DataGridView1.Columns.Count - 1
If k = DataGridView1.Columns.Count - 1 Then
DataRow = DataRow & DataGridView1.Rows(i).Cells(k).Value ' last column dont put , separate
End If
DataRow = DataRow & DataGridView1.Rows(i).Cells(k).Value & ","
Next
file.WriteLine(DataRow)
DataRow = ""
Next
loopend:
file.Close()
End Sub