Results 1 to 8 of 8

Thread: [2005] - For Loop In DataGridView

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Question [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] - For Loop In DataGridView

    1.
    vb.net Code:
    1. For Each row As DatagridViewRow In myDataGridView.Rows
    2.     MessageBox.Show(CStr(row.Cells(1).FormattedValue))
    3. 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:
    1. For rowIndex As Integer = 0 To myDataGridView.Rows.Count - 1 Step 1
    2.     For columnIndex As Integer = 0 To myDatagridView.Columns.Count - 1 Step 1
    3.         If columnIndex > 0 Then
    4.             'Write a comma if an only if this is NOT the first column.
    5.  
    6.             If columnIndex = 3 OrElse columnIndex = 4 Then
    7.                 'Write a space in the fourth and fifth columns.
    8.             End If
    9.         End If
    10.  
    11.         'Write the cell value.
    12.     Next columnIndex
    13. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Lively Member ComITSolutions's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    94

    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    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

  5. #5
    Lively Member ComITSolutions's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    94

    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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Question 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.

  7. #7
    Lively Member ComITSolutions's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    94

    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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    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
  •  



Click Here to Expand Forum to Full Width