Hi,
I am new to VB.net programming and was wondering if anybody has any suggestions as to how I can export data from a DataGridView via a command button to a CSV file.
Any suggestions or sample code would be really appreciated.
Many thank.
Paul.
10 63057
Did u get the solution of your problem???
Actually I am also required the same kind of solution.
If you u get that plz forword that to me.
Hi, I was working with CSV file and DataGridView in VB.NET. Having lots fo complexities i succeeded to retrieve a 5 columns CSV file into DataGridView. It is to be mentioned that the last two columns in the dgv are Combo type.
My goal was after retrieving the csv file I will edit other two selection columns and export the modified file in a different locations.
the content of the CSV file is as follows:
2014001,"Mensch, Samantha",CD Models Christian values,S,*
2014001,"Mensch, Samantha",CD Shows reverence during pray,S,
2014001,"Mensch, Samantha",CD Accepts responsibility for,S,
2014001,"Mensch, Samantha",CD Practices self control,S,
2014001,"Mensch, Samantha",CD Observes school rules,S,*
2014001,"Mensch, Samantha",CD Respects property,S,
2014001,"Mensch, Samantha",CD Respects others,S,
2014001,"Mensch, Samantha",Uses time wisely,S,
2014001,"Mensch, Samantha",Displays effort,S,
2014001,"Mensch, Samantha",Works well in a group,S,
2014001,"Mensch, Samantha",Works well independently,S,
Code for the Reading CSV file as follows under a button:
Sub Action_Button
Dim lineNumber As Integer = 0
Dim line As String
Dim lToken() As String
Dim sTemp As String
Dim sGridRow(4) As String
OpenFileDialog1.InitialDirectory = "c:\temp\"
'openFileDialog1.Filter = "All files (*.*)|*.*"
OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
fName = OpenFileDialog1.FileName
Me.Text = fName
End If
Me.Text = fName
Try
Dim reader As IO.StreamReader = New IO.StreamReader(fName)
'DataGridView2.Rows.Remove()
DataGridView2.Rows.Clear()
While reader.Peek <> -1
line = reader.ReadLine()
lToken = Split(line, ",")
For i As Integer = 0 To lToken.Length - 3
sTemp = Replace(lToken(i), """", "")
sGridRow(i) = sTemp
Next
Me.DataGridView2.Rows.Add(sGridRow)
lineNumber += 1
End While
'Me.Text = Str(lineNumber)
reader.Close()
Catch ex As Exception
MessageBox.Show("Error occured while reading the csv file." + ex.ToString)
End Try
End Sub
The above code works fine, Please see the attached.
Now I face the problem after modifying the row in the dgv; suppose I have selected a grade from the combo. Now I want to export the file in a selected location as with the same name as I imported.
I can import the file taking the data from the dvg. I gave the code here to populate the dvg rows. I need entire dvg data to read and write them in the file.
The code I don know...How to take the data from the dbg from the first row to the last? The code I wrote is pasted here, but it simply populates the current row column.
Private Sub SaveGridDataInFile(ByRef fName As String)
'Dim writer As IO.StreamWriter = New IO.StreamWriter(fName)
Dim I As Integer = 0
Dim j As Integer = 0
Dim cellvalue$
Dim rowLine As String = ""
Try
FileOpen(1, fName, OpenMode.Output) ' Open file for output.
'WriteLine(1, "Hello", " ", "World") ' Separate strings with space.
'WriteLine(1, SPC(5), "5 leading spaces ") ' Print five leading spaces.
'WriteLine(1, TAB(10), "Hello") ' Print word at column 10.
'FileClose(1) ' Close file.
'sData = DataGridView1.Columns(1).Name.ToString()
'sData = Me.DataGridView1.CurrentRow.Cells(i).Value.ToStrin g
'MessageBox.Show(sData)
For j = 0 To (DataGridView2.Rows.Count - 2)
For I = 0 To (DataGridView2.Columns.Count - 1)
If Not TypeOf DataGridView2.CurrentRow.Cells.Item(I).Value Is DBNull Then
cellvalue = DataGridView2.CurrentRow.Cells.Item(I).Value.ToStr ing
Else
cellvalue = ""
End If
'DataGridView2.CurrentRow.Cells.Item(I).Value = cellvalue
rowLine = rowLine + cellvalue + ","
Next
WriteLine(1, TAB(0), rowLine) ' Print word at column 10.
'writer.WriteLine()
rowLine = ""
Next
FileClose(1) ' Close file.
Catch e As Exception
MessageBox.Show("Error occured while writing to the file." + e.ToString())
Finally
FileClose(1)
End Try
End Sub
Could you please help me giving the code that reads entire row column of the dvg?
At last i succeeded. That is I populated the datagridview control with the following syntax:
For j = 0 To (DataGridView2.Rows.Count - 2)
For I = 0 To (DataGridView2.Columns.Count - 1)
If Not TypeOf DataGridView2.Item(I, j).Value Is DBNull Then cellvalue = DataGridView2.Item(I, j).Value Else
cellvalue = ""
End If
rowLine = rowLine + "" & Chr(34) & cellvalue & Chr(34) + ","
Next
rowLine = rowLine.Remove(rowLine.Length - 1, 1)
sw.WriteLine(rowLine)
rowLine = ""
Next
Now I can export CSV file from datagrideview to a formated text file.
[EMAIL REMOVED]
Please give the subject of the email: CSF file Handler.
Thanks and regards
Shahidul Haque.
Did u get the solution of your problem???
Actually I am also required the same kind of solution.
If you u get that plz forword that to me.
Yes I did get a solution to my problem...I know the code may seem a little complicated but it works a treat, obviously you will need to tweak any data but this is the code I needed for my solution.
Private Sub btnExportData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportData.Click
Dim fsStream As New FileStream("C:\Temp\Test Results_" & txtTestSpecID.Text & "_" & cboTestPhase.Text & ".csv", FileMode.Append, FileAccess.Write)
Dim swWriter As New StreamWriter(fsStream)
Dim TotalPasses1 As Integer = 0
Dim TotalFails1 As Integer = 0
Dim TotalNA1 As Integer = 0
'Displays a Message Box informing the user you are about to Quit the application
Dim intReturnValue1 As Integer
intReturnValue1 = MsgBox("Data Saved Successfully", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Data Saved")
Try
CheckSave()
swWriter.WriteLine("Database, Test Spec ID, System, Version, Test Phase, Sites, Date/Time, Tester, Witness,Test Number, Test Step, Step 1, Step 2, Pass, Fail, N/A, Fault Reference,Fault Category,Comments,")
Dim temp As Integer = 0
For temp = 0 To grdResults.Rows.Count - 1
swWriter.Write(lblTestsSpec.Text & "," & txtTestSpecID.Text & "," & cboSystem.Text & "," & txtVersion.Text & "," & cboTestPhase.Text & "," & cboSites.Text & "," & txtDateTime.Text & "," & txtTester.Text & "," & txtWitness.Text & ",")
Dim temp2 As Integer = 0
For temp2 = 0 To grdResults.Rows(temp).Cells.Count - 1
If IsDBNull((grdResults.Rows.Item(temp).Cells.Item(te mp2).Value)) Then
Else
If (grdResults.Rows.Item(temp).Cells.Item(temp2).Valu e) = "True" Then
swWriter.Write("1")
If temp2 = 4 Then
TotalPasses1 = TotalPasses1 + 1
End If
If temp2 = 5 Then
TotalFails1 = TotalFails1 + 1
End If
If temp2 = 6 Then
TotalNA1 = TotalNA1 + 1
End If
ElseIf (grdResults.Rows.Item(temp).Cells.Item(temp2).Valu e) = "False" Then
swWriter.Write("")
Else
swWriter.Write(grdResults.Rows.Item(temp).Cells.It em(temp2).Value)
End If
End If
swWriter.Write(",")
Next
swWriter.WriteLine(",")
Next
swWriter.WriteLine("," & "," & "," & "," & "," & "," & "," & "," & "," & "," & "," & "," & "," & TotalPasses1 & "," & TotalFails1 & "," & TotalNA1 & ",")
swWriter.Flush()
MsgBox("Data Saved Successfully")
swWriter.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
hi
what is a csv file????
thanks in advance
Hi I am little bit confused about your question.
You wanted to know what is a CSV file? if this is the question, the simple answer is it is a Comma Separated Value file almost like a text file. It can be opened in XLS format also. and to open it in notepade is trivial.
Paste these lines in a text file and change the file name for example text.csv.
Try to open it in MS Excel and then see the csv file.
2014001,"Mensch, Samantha",CD Models Christian values,S,*
2014001,"Mensch, Samantha",CD Shows reverence during pray,S,
2014001,"Mensch, Samantha",CD Accepts responsibility for,S,
2014001,"Mensch, Samantha",CD Practices self control,S,
2014001,"Mensch, Samantha",CD Observes school rules,S,*
2014001,"Mensch, Samantha",CD Respects property,S,
2014001,"Mensch, Samantha",CD Respects others,S,
2014001,"Mensch, Samantha",Uses time wisely,S,
2014001,"Mensch, Samantha",Displays effort,S,
2014001,"Mensch, Samantha",Works well in a group,S,
2014001,"Mensch, Samantha",Works well independently,S,
Thanks.
shaque
First of all thank u very much for giving some solution.
Actual my moto is to save the datagridvalues in any file(.txt /.csv).
I have already able to save the data in .txt file by using the simple code mentioned below:
using (StreamWriter MyFile = new StreamWriter(Application.StartupPath+"\\About File Log.txt"))
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
MyFile.WriteLine("File Name: " + FinalDirs[i] + ", " + "File Version: " + FinalVers[i] + ", " + "File Size: " + FinalSize[i] + ", " + "Description: " + FinalDesc[i]);
MyFile.WriteLine("");
// Arbitrary objects can also be written to the file.
}
MyFile.Write("This file generated on: ");
MyFile.WriteLine(DateTime.Now);
MessageBox.Show("Information of all the files are successfully saved in the following location: \n"+Application.StartupPath+"\\AboutFileLog.txt" , Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
Close();
}
}
hi
what is a csv file????
thanks in advance
A csv file is a comma seperated value, basically in laimans terms a text file that is easily imported into an Excel style spreadsheet.
Hope that makes things a little clearer.
A csv file is a comma seperated value, basically in laimans terms a text file that is easily imported into an Excel style spreadsheet.
Hope that makes things a little clearer.
Hi, ... if it is helpfull you can use DataGridView Extension for .NET to export data to excel, html .... soon pdf. Also you can search for text after adding the component, to save settings like themes, to apply styles, manage columns and so on.
here it is the site http://www.completit.com/Products/DGVE/Overview.aspx
Hi
What I would like to know is how do you populate a COMBOBOX cell with data from a CSV file?
Has anybody done that before?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: atse |
last post by:
Hi experts,
I retrieve data from the database and display on ASP, then I export these
data to a file, like Excel (the best) or text file. Is it possible? I think
it is possible, but how can I do...
|
by: Thomas Massong |
last post by:
Hello,
i have a problem with an ixf file and need your help.
Is there any way to export such a file to ascii or some other readable
format for a Windows PC?
Unfortunaly we have no other way...
|
by: Oliver Stratmann |
last post by:
Hello all,
is there another way to export data to a csv-file (comma-separated format)?
We tried the EXPORT-command and got the following error
"
SQL1325N The remote database environment does...
|
by: Paolo |
last post by:
Hello,
I am trying to create a procedure to export my Access data to a word
template.
I would like to export data from two tables to a word file at the same
time. My two tables are named CASES...
|
by: pauljohns353 |
last post by:
Hi,
I am new to VB.net programming and was wondering if anybody has any suggestions as to how I can export data from a DataGridView via a command button to a CSV file.
Any suggestions or sample...
|
by: Max2006 |
last post by:
Hi,
We are developing a SQL server based asp.net application. As part of
requirement we should allow users import/export some relational data through
web user interface. We are investigation...
|
by: =?Utf-8?B?YzY3NjIyOA==?= |
last post by:
Hi all,
I have a question for you.
I have a .csv file which has many lines of data.
Each line has many data fields which are delimited by ",".
Now I need to extract part of data from this...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |