|
-
Feb 27th, 2008, 10:01 PM
#1
Thread Starter
Lively Member
[2005] - For Loop In DataGridView
Hi All,
This post actually involves 2 questions
Question 1:
I would like to loop through column 2 of the datagridview so that each value can be placed into a string. Any suggestions on the for loop?
Question 2
In the second datagridview, I will be extracting all values to a CSV File(That part is complete). In the CSV file, I want to put a space after the 3rd and 4th comma. I would also like to remove the last comma on each line.
-
Feb 27th, 2008, 10:44 PM
#2
Re: [2005] - For Loop In DataGridView
1.
vb.net Code:
For Each row As DatagridViewRow In myDataGridView.Rows MessageBox.Show(CStr(row.Cells(1).FormattedValue)) Next row
2. If you have a comma on the end of each line then you're doing it wrongly in the first place.
vb.net Code:
For rowIndex As Integer = 0 To myDataGridView.Rows.Count - 1 Step 1 For columnIndex As Integer = 0 To myDatagridView.Columns.Count - 1 Step 1 If columnIndex > 0 Then 'Write a comma if an only if this is NOT the first column. If columnIndex = 3 OrElse columnIndex = 4 Then 'Write a space in the fourth and fifth columns. End If End If 'Write the cell value. Next columnIndex Next rowIndex
You could also write the comma after the value and test that the current column was not the last, rather than writing it before and testing it was not the first.
-
Feb 27th, 2008, 10:54 PM
#3
Lively Member
Re: [2005] - For Loop In DataGridView
Ans1:
some of the properties of Datagridview that u can use, RowCount,ColumnCount
to Know the No of Rows and columns. U can access value in any cell by
Datagridview(Col,Row).Value
SomeVal=Datagridview(Col,Row).Value
Datagridview(Col,Row).Value=SomeVal
Ans2:
If u post some code. It would be easy to analyse and suggest u the solution.
Encourage the fellow member’s efforts by rating
- ComIT Solutions
-
Feb 28th, 2008, 12:22 AM
#4
Thread Starter
Lively Member
Re: [2005] - For Loop In DataGridView
Code for exporting datagridview
Code:
Private Sub ExportGrid()
Using sfd As New SaveFileDialog()
sfd.Filter = "CSV File|*.csv*"
If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim I As Integer = 0
Dim J As Integer = 0
Dim cellvalue$
Dim rowLine As String = ""
Try
Dim objWriter As New System.IO.StreamWriter(sfd.FileName & ".csv", True)
For J = 0 To (DataGridViewData.Rows.Count - 2)
For I = 0 To (DataGridViewData.Columns.Count - 1)
If Not TypeOf DataGridViewData.CurrentRow.Cells.Item(I).Value Is DBNull Then
cellvalue = DataGridViewData.Item(I, J).Value
Else
cellvalue = ""
End If
rowLine = rowLine + cellvalue + ","
Next
objWriter.WriteLine(rowLine)
rowLine = ""
Next
objWriter.Close()
MsgBox("Data written to CSV File...")
Catch ex As Exception
MsgBox("An error occured while writing to the CSV file..." + ex.ToString())
End Try
End If
End Using
End Sub
I'm now comparing the code I found on the net with jmcilhinney's code to see if I can find a solution
Thanks jmcilhinney for your answer to question 1... Worked like a charm
-
Feb 28th, 2008, 12:37 AM
#5
Lively Member
Re: [2005] - For Loop In DataGridView
Question 2
In the second datagridview, I will be extracting all values to a CSV File(That part is complete). In the CSV file, I want to put a space after the 3rd and 4th comma. I would also like to remove the last comma on each line
Code:
For J = 0 To (DataGridViewData.Rows.Count - 2)
For I = 0 To (DataGridViewData.Columns.Count - 1)
If Not TypeOf DataGridViewData.CurrentRow.Cells.Item(I).Value Is DBNull Then
cellvalue = DataGridViewData.Item(I, J).Value
Else
cellvalue = ""
End If
rowLine = rowLine & cellvalue & IIf(I< DataGridViewData.Columns.Count - 1,",","") '<---I would also like to remove the last comma on each line
if i = 2 or i= 3 Then ' Since colums starts from 0
RowLine=rowLine & " " '<---I want to put a space after the 3rd and 4th comma.
End if
Next
objWriter.WriteLine(rowLine)
rowLine = ""
Next
Encourage the fellow member’s efforts by rating
- ComIT Solutions
-
Feb 28th, 2008, 07:49 PM
#6
Thread Starter
Lively Member
Re: [2005] - For Loop In DataGridView
Thanks ComITSolutions for your solution. Worked perfectly
There is one problem that I have run into though. When I am reading my CSV file into the datagridview, there is always a blank row in the datagridview. When I read the values from column 2 into the second datagridview using the website datascraper for loop, the last row of the datagridview has random data in it as the for loop is sending an empty value to the website.
Code to read CSV File: (Found On CodeProject.com)
Code:
Dim textline as string = ""
Dim SplitLine() as string
If system.IO.File.Exists(fname) = true then
Dim obj As New IO.StreamReader(fname)
Do While obj.Peek() <> -1
textline = obj.readline()
SplitLine = Split(textline, ",")
Me.DataGridView1.Rows.Add(SplitLine)
Loop
Else
MsgBox("File Not Found...")
End IF
I'm also trying to implement a progress bar when the website datascraper is running. I'm following this article Using the BackgroundWorker Component but getting stuck at
Code:
worker.ReportProgress(i, i & " iterations complete")
Last edited by nolim8ts; Feb 28th, 2008 at 09:05 PM.
-
Feb 28th, 2008, 11:32 PM
#7
Lively Member
Re: [2005] - For Loop In DataGridView
Code:
Dim textline as string = ""
Dim SplitLine() as string
If system.IO.File.Exists(fname) = true then
Dim obj As New IO.StreamReader(fname)
Me.DataGridView1.AllowUserToAddRows = False '<--- Add this line to avoid junk in last row
Do While obj.Peek() <> -1
textline = obj.readline()
SplitLine = Split(textline, ",")
Me.DataGridView1.Rows.Add(SplitLine)
Loop
Else
MsgBox("File Not Found...")
End IF
Encourage the fellow member’s efforts by rating
- ComIT Solutions
-
Feb 29th, 2008, 01:16 AM
#8
Thread Starter
Lively Member
Re: [2005] - For Loop In DataGridView
Thanks ComITSolutions for the solution . Now to sort out the problem with the background worker reporting its progress (The application locks up after scraping all the data )
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|