VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,358 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,899 people online right now. Registration is fast and FREE... Join Now!




Problem with data grid

 

Problem with data grid, only last rows got text in it

NoBrain

3 Jul, 2009 - 01:08 AM
Post #1

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
hi again some stupid questions from me !!!

i got a test of mine of reading text file and fill it data grid but when i do this code only cells in last row is full

CODE

Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Dim objReader As New StreamReader("c:\test.txt")
        Dim strLine As String
        Dim strArry As New ArrayList()
        Dim i As Integer = 0
        Dim nRow As Integer = 1
        Dim l As Integer
        Dim p As Integer = 1
        Dim strCols As String
        Dim Len_str As Integer
        Dim nCol As Integer = 0
        Dim dblPrice As Double



        Do
            strLine = objReader.ReadLine
            If Not strLine Is Nothing Then
                strArry.Add(strLine)
                i = i + 1
            End If
        Loop Until strLine Is Nothing

        For Each strLine In strArry
            grdTest.Rows.Add(1)
            Len_str = Len(strLine)
            strCols = Mid(strLine, 1, 3)

            Do
                l = InStr(p, strLine, "|")
                strCols = Mid(strLine, p, l - p)
                grdTest.Rows(nRow).Cells(nCol).Value = strCols


                If nCol = 3 Then
                    dblPrice = (((strCols * 20) / 100) + strCols)
                    grdTest.Rows(nRow).Cells(4).Value = dblPrice
                End If
                nCol = nCol + 1
                p = l + 1
            Loop Until (Len_str = l)
            nCol = 0
            nRow = nRow + 1
            p = 1
        Next

    End Sub


ty for your help

User is offlineProfile CardPM
+Quote Post


modi123_1

RE: Problem With Data Grid

3 Jul, 2009 - 08:58 AM
Post #2

Suiter #2
Group Icon

Joined: 12 Jun, 2008
Posts: 1,760



Thanked: 72 times
Dream Kudos: 150
My Contributions
My first question - is why did you take this route to loading a text file to the grid? It is overly cumbersome.
User is online!Profile CardPM
+Quote Post

NoBrain

RE: Problem With Data Grid

4 Jul, 2009 - 01:57 AM
Post #3

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
well i want to try it still why is this happening only the cells in last row got text in it? any one can help is there something i miss may be property or something
User is offlineProfile CardPM
+Quote Post

noorahmad

RE: Problem With Data Grid

4 Jul, 2009 - 03:45 AM
Post #4

Webmaster
Group Icon

Joined: 12 Mar, 2009
Posts: 2,018



Thanked: 125 times
Dream Kudos: 1350
My Contributions
Where is what is your error and could you please attach your text file
User is offlineProfile CardPM
+Quote Post

NoBrain

RE: Problem With Data Grid

6 Jul, 2009 - 09:39 AM
Post #5

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
no errors no nothing just dont fill the first rows

the txt file got lines like this

id|name|secondname|2.50|

4 rows but only the last is full i debug it and in strCols i got what i want but not in cell sad.gif help pls
User is offlineProfile CardPM
+Quote Post

modi123_1

RE: Problem With Data Grid

6 Jul, 2009 - 10:49 AM
Post #6

Suiter #2
Group Icon

Joined: 12 Jun, 2008
Posts: 1,760



Thanked: 72 times
Dream Kudos: 150
My Contributions
The problem is you are not tracking the 'added row' index right.

You are always over writing the same row - the 'add row' pushes the current row to the next line.

Simply putting

CODE
            nRow = grdTest.Rows.Add(1)


and removing the
CODE
nRow = nRow + 1

instead of
CODE
            grdTest.Rows.Add(1)

and incrementing it yourself fixes your issue.

Here's my code for the solution - a bit more robust.
CODE

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objReader As New StreamReader("c:\test.txt")
        Dim strLine As String
        Dim strArry As New ArrayList()
        Dim i As Integer = 0

        Do
            strLine = objReader.ReadLine
            If Not strLine Is Nothing Then
                strArry.Add(strLine)
                i = i + 1
            End If
        Loop Until strLine Is Nothing

'-- --
        Dim lCurrentColumn As Int32 = 0
        Dim lStart As Int32 = 0
        Dim lAddedRow As Int32 = 0

        For Each tempLine As String In strArry
            lAddedRow = Me.grdTest.Rows.Add(1)
            lCurrentColumn = 0
            lStart = 0
            For a As Int32 = 0 To tempLine.Count - 1
                If tempLine(a) = "|" Then
                    grdTest.Rows(lAddedRow).Cells(lCurrentColumn).Value = tempLine.Substring(lStart, a - lStart)
                    lCurrentColumn += 1
                    lStart = a + 1
                End If
            Next

        Next


    End Sub

User is online!Profile CardPM
+Quote Post

NoBrain

RE: Problem With Data Grid

6 Jul, 2009 - 12:20 PM
Post #7

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
ty for this dude rly i am so new that i cant fill a grid sad.gif but i try to lear ty again for all your help
User is offlineProfile CardPM
+Quote Post

modi123_1

RE: Problem With Data Grid

6 Jul, 2009 - 12:21 PM
Post #8

Suiter #2
Group Icon

Joined: 12 Jun, 2008
Posts: 1,760



Thanked: 72 times
Dream Kudos: 150
My Contributions
QUOTE(NoBrain @ 6 Jul, 2009 - 02:20 PM) *

ty for this dude rly i am so new that i cant fill a grid sad.gif but i try to lear ty again for all your help


No problem. You might want to invest some time in looking at making a dataset (with table) and hooking that up to the grid and filling the dataset instead of the grid directly. Your data is in a more flexible location then.
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 07:38PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month