Row Count problem

DekaFlash

Well-known member
Joined
Feb 14, 2006
Messages
117
Programming Experience
1-3
I am trying to add a delimiter to my datagrid, except instead of adding it to the desired row, it adds it to the preceding or proceeding row.

Can you please assist?

Thanks

VB.NET:
If (EOR_Limit = 0) Then
    EOR_Limit = Me.DataGridView1.Rows.Count - 2 + CDbl(RaceRunners.Text)
    EOR_Count = (Me.DataGridView1.Rows.Count - 2) + 1
Else
    ' Do Nothing
End If

If (EOR_Count = EOR_Limit) Then
   If (EOR_Count = 0) Then
      ' Do Nothing
   Else
      Me.DataGridView1.Rows(Me.EquinexMasterSqlDataSet.Tables  
      ("VsrData").Rows.Count - 1).Cells.Item(20).Value = "Y"

      Me.DataGridView1.Update()
  End If
End If
EOR_Count += 1

Me.DataGridView1.Update()
Me.VsrDataTableAdapter.Update(Me.EquinexMasterSqlDataSet.VsrData)
 
Last edited by a moderator:
Argh.. You do not add rows to a grid. Grids dont store data any more, they just show and edit data they find in an underlying model. For lots of info on the concept, wiki for MVC (model view controller).

Bind your grid to a rectangular data source like a DataTable, and edit that. Changes will show in the grid

I dont know what "add a delimiter" means, but simply calling:

myDataSet.MyXXXDataTable.AddMyXXXRow("this", "row", "is", "a", "delimiter") would add a row to the bottom of the table. remember that grids sort rows independently of the order of the rows in the model (sorting is a function of the View, not the Model - read up about MVC)



Me.EquinexMasterSqlDataSet.Tables("VsrData")
No, no! That's a typed datatable and you just forced it generic! Access it like this:
Me.EquinexMasterSqlDataSet.VsrData

to maintain the type specific aspect without casting

That said.. i'm very impresse that youre getting on with the tableadapter lark-> new way to do data acces sin 2005. Makes a nice change for a poster to come here and not be pasting some SQL into a button handler
 
In case I didn't explain myself clearly earlier, I have pasted all the relevant code.

The included code will demonstrate how the row is getting added to the data table.

I have also attached a screen capture, which I hope will explain my problem easier.

1) The column to the right of where I put 1 at the bottom of the grid should precede the rows with 2 to 7 in the column before it.

2) Where I put 2 is where I am trying to put a delimiter of Y, I specify how much data I will be adding and when it reaches the last piece of data I wish to include a way of
identifying this.

I've changed the line to Me.EquinexMasterSqlData.VsrData as you suggested, thanks for your praise regarding table adapters, that's good to hear especially considering this is
all new to me :)

VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewData.Click

        FlatStr = ""
        HurdleStr = ""
        ChaseStr = ""
        HcapStr = ""

        VsrDataRow = Me.EquinexMasterSqlDataSet.VsrData.NewRow()

        RowCount = Me.EquinexMasterSqlDataSet.VsrData.Rows.Count

        VsrDataRow.Time = Me.RaceTime.Text
        VsrDataRow._Date = Me.RaceDate.Text
        VsrDataRow.Venue = Me.RaceVenue.Text
        VsrDataRow.RecordId = Me.RaceVenue.Text + Me.RaceDate.Text.ToCharArray(0, 
        10) + "-" + Me.RaceTime.Text.ToCharArray(0, Me.RaceTime.TextLength) + "-" + 
        RowCount.ToString

        VsrDataRow.CHF = Me.RaceCHF.Text
        VsrDataRow.Hcap = Me.RaceHcap.Text
        VsrDataRow.RaceDist = Me.RaceDistance.Text
        VsrDataRow.RaceRunners = Me.RaceRunners.Text
        EOR = CInt(Me.RaceRunners.Text)
        VsrDataRow._Class = Me.RaceClass.Text
        VsrDataRow.Going = Me.RaceGoing.Text

        If (Me.RaceCHF.Text = "F") Then
            FlatStr = "Flat"
        ElseIf (Me.RaceCHF.Text = "H") Then
            HurdleStr = "Hurdle"
        ElseIf (Me.RaceCHF.Text = "C") Then
            ChaseStr = "Chase"
        Else
            ' Do Nothing
        End If

        If (Me.RaceHcap.Text = "Y") Then
            HcapStr = "Handicap"
        Else
            ' Do Nothing
        End If

        VsrDataRow.RaceTitle = FlatStr + "" + HurdleStr + "" + ChaseStr + " " + HcapStr 
        + " " + TitlesDebug.Text

        VsrDataRow.SaddleCloth = "1"
        VsrDataRow.HorseName = "Horses"
        VsrDataRow.Lf3 = "1"
        VsrDataRow.Lf2 = "1"
        VsrDataRow.Lf1 = "1"
        VsrDataRow.LastRan = "2"
        VsrDataRow.BettingPos = "1"
        VsrDataRow.FcPrice = "9/2"

        VsrDataRow.TrainerForm = "1"
        VsrDataRow.GoingForm = "1"
        VsrDataRow.DistForm = "1"
        VsrDataRow.CourseForm = "1"
        VsrDataRow.DrawForm = "-"
        VsrDataRow.AbilityForm = "1"
        VsrDataRow.FormForm = "1"
        VsrDataRow.PS = "1"
        VsrDataRow.Rating = "1"
        VsrDataRow.FDB = "1"
        VsrDataRow.B = "1"
        VsrDataRow.Jockey = "1"
        VsrDataRow.Trainer = "1"
        VsrDataRow.TravCheck = "1"
        VsrDataRow.Bolds = "1"
        VsrDataRow.Arrows = "1"

        If Count = 0 Then
            Me.EquinexMasterSqlDataSet.VsrData.Rows
           (Me.EquinexMasterSqlDataSet.VsrData.Rows.Count - 1).Item(10) = FlatStr + ""  
           + HurdleStr + "" + ChaseStr + " " + HcapStr + " " + TitlesDebug.Text
        Else
            Me.EquinexMasterSqlDataSet.VsrData.Rows
            (Me.EquinexMasterSqlDataSet.VsrData.Rows.Count - 1).Item(10) = FlatStr + "" 
            + HurdleStr + "" + ChaseStr + " " + HcapStr + " " + TitlesDebug.Text
        End If

        If (EOR_Limit = 0) Then
            EOR_Limit = (Me.EquinexMasterSqlDataSet.VsrData.Rows.Count - 2) + CInt
            (RaceRunners.Text)
            EOR_Count = (Me.EquinexMasterSqlDataSet.VsrData.Rows.Count - 2)
        Else
            ' Do Nothing
        End If

        ----> This refers to 1 in the explanation above/screen capture.

        Me.EquinexMasterSqlDataSet.VsrData.Rows
        (Me.EquinexMasterSqlDataSet.VsrData.Rows.Count - 1).Item(12) = ScCount
        
        MsgBox(EOR_Count.ToString + " " + EOR_Limit.ToString)
        If (EOR_Count = EOR_Limit) Then
            If (EOR_Count = 0) Then
                ' Do Nothing
            Else
                ----> This refers to 2 in the explanation above/screen capture.

                Me.EquinexMasterSqlDataSet.VsrData.Rows
                (Me.EquinexMasterSqlDataSet.VsrData.Rows.Count - 2).Item(20) = "Y"
                Me.DataGridView1.Update()
            End If
        End If
        EOR_Count += 1

        Me.EquinexMasterSqlDataSet.VsrData.Rows.Add(VsrDataRow)
        Me.DataGridView1.Update()
        Me.VsrDataTableAdapter.Update(Me.EquinexMasterSqlDataSet.VsrData)

        ScCount += 1
    End Sub

DataGridProblem.jpg
 
Last edited:
1) The column to the right of where I put 1 at the bottom of the grid should precede the rows with 2 to 7 in the column before it.
You mean the grid should be sorted by that column? (possibly in combination with another column also.. but thats academic)

2) Where I put 2 is where I am trying to put a delimiter of Y, I specify how much data I will be adding and when it reaches the last piece of data I wish to include a way of
identifying this.
THis doesnt make a lot of sense to me, to be honest

VsrDataRow.RecordId = Me.RaceVenue.Text + Me.RaceDate.Text.ToCharArray(0,
10) + "-" + Me.RaceTime.Text.ToCharArray(0, Me.RaceTime.TextLength) + "-" +
RowCount.ToString
...
VsrDataRow.RaceTitle = FlatStr + "" + HurdleStr + "" + ChaseStr + " " + HcapStr
+ " " + TitlesDebug.Text
Uhmm.. The operator for concatenating strings in VB.NET is &, not +.. mmkay? :)

Try this, actually, its better way to build strings:

VsrDataRow.RaceTitle = String.Format("{0}{1}{2} {3} {4}", FlatStr, HurdleStr, ChaseStr, HcapStr, TitlesDebug.Text)


Dim s as String = myString1 & myString2

This will stop VB trying to convert strings to numbers when you "numerical string" + "numerical string" them

"1" & "2" = "12"
"1" + "2" = "3", or 3, or something like that.. i dunno.. its not soemthing I ever did




If Count = 0 Then
Me.EquinexMasterSqlDataSet.VsrData.Rows
(Me.EquinexMasterSqlDataSet.VsrData.Rows.Count - 1).Item(10) = FlatStr + ""
+ HurdleStr + "" + ChaseStr + " " + HcapStr + " " + TitlesDebug.Text

Um... if COunt = 0, then youre gonna subt6ract 1 from it, and then try and access that rowid and do what?? There is no row at position -1

Note.. We dont put rows in datatables in order! Sorting is a function of the DataView or the Bindingsource, NOT the datatable. The table holds data, the view sorts and filters it and the grid shows it. If you said:

MyGrid.DataSource = MyDataTable

then actually, the grid is looking at the MyTable.DefaultView view.. sort that! and also set the sort order on soemthing sensible. You make rows appear where you want them to appear NOT by inserting them into the table in certain places but simply by inserting them any old place but with the correct sorted columns set up so they appear where you want!


(Me.EquinexMasterSqlDataSet.VsrData.Rows.Count - 2).Item(20) = "Y"
Again.. we dont really refer to Item(20) because that's generic. You should at least try to keep the type specific stuff, which is default property stuff..


Example:
MyDataTable.Rows(0) ' returns a DataRow, which is a generic supertype of a MyDataTableRow
MyDataTable(0) ' returns the type specific row


If you dont know much about OO languages, take some time out to read on Boxing.. Boxing is where you take a parent and assigna child to it. ALl the child specialities are hidden, but still available via casting:

Dim p as ParentType = New ChildType 'child inherits from parent

p.ParentProperty 'available
p.ChildProperty 'not available
DirectCast(p, ChildTYpe).ChildProperty 'available because it is actually a hild type stored in the parent variable, we jsut use a cast to unbox it

So
MyDataTable(0) returns you the first child row.. and its type specific.Suppose the column at item(20) is called HorseName:

MyVsrDataTable(10).HorseName 'available, row returned from indexer is type specific
MyVsrDataTable.Rows(10).HorseName 'not available; row returned is a child, but boxed up in parent type DataRow which has no HorseName property

DirectCast(MyVsrDataTable.Rows(10), MyVsrDataRow).HorseName ' available, but what a mess and long to type when we can just use the simpler form from the first line

Me.DataGridView1.Update()
Should be unnecessary
 
Back
Top