VS 2010 Adding rows to bottom of datagrid view-VBForums
Results 1 to 4 of 4

Thread: Adding rows to bottom of datagrid view

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    39

    Adding rows to bottom of datagrid view

    I have what should be a simple problem to fix.

    I am trying to insert an initial line (0) into a datagridview (named params). The program will then read each cell in that row, pass them through a function and return values be placed into the next line (1). This will continue until one of the parameters reaches some set point.

    The problem that I am having is that when I try to insert a new line, it appears above the initial one and not below it like I would like it to.

    Here's my code so far:

    Code:
    1. Private Sub HurricaneWinds_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim init_xrange() As Double = New Double() {0.0, 0.0, 0.0, 40.0, 0.0}
    3.  
    4.        
    5.         For i As Integer = 0 To 4
    6.             params(i, 0).Value = init_xrange(i)
    7.         Next
    8.  
    9.         Dim xx(4) As Double
    10.        
    11.  
    12.         For j As Integer = 1 To 10
    13.             Dim prange() As Double = New Double() {CDbl(DragCo.Text.Trim), CDbl(WindVelCoef.Text.Trim), CDbl(WindVelExp.Text.Trim), CDbl(Gravity.Text.Trim)}
    14.             Dim xrange() As Double = New Double() {CDbl(params(0, j - 1).Value), CDbl(params(1, j - 1).Value), CDbl(params(2, j - 1).Value), CDbl(params(3, j - 1).Value), CDbl(params(4, j - 1).Value)}
    15.             xx = rk4(CInt(NumTimesteps.Text.Trim), CDbl(TimeStep.Text.Trim), xrange, prange)
    16.            
    17.             Dim item As New DataGridViewRow
    18.             item.CreateCells(params)
    19.             With item
    20.                 .Cells(0).Value = xx(0)
    21.                 .Cells(1).Value = xx(1)
    22.                 .Cells(2).Value = xx(2)
    23.                 .Cells(3).Value = xx(3)
    24.                 .Cells(4).Value = xx(4)
    25.             End With
    26.             params.Rows.Add(item)
    27.            
    28.         Next
    29.  
    30.     End Sub

    And a picture of the problem:

    Name:  rows.JPG
Views: 499
Size:  21.8 KB

    Any suggestions?

    Thanks

  2. #2
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    5,696

    Re: Adding rows to bottom of datagrid view

    Why not add the rows as follows where the DataGridView columns are predefined.

    Code:
    YourDataGridViewName.Rows.Add(New Object() {xx(1),xx(2),xx(3),xx(4)})

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    39

    Re: Adding rows to bottom of datagrid view

    Quote Originally Posted by kevininstructor View Post
    Why not add the rows as follows where the DataGridView columns are predefined.

    Code:
    YourDataGridViewName.Rows.Add(New Object() {xx(1),xx(2),xx(3),xx(4)})
    Mainly because I don't know how many rows are going to need to be created. Depending on the input parameters it could vary between 5 and hundreds.

    My intention is to create a new row on the each time it goes through the For loop (which I will eventially convert to a 'do while' once this is working).

  4. #4
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    5,696

    Re: Adding rows to bottom of datagrid view

    Quote Originally Posted by catoctinwx View Post
    Mainly because I don't know how many rows are going to need to be created. Depending on the input parameters it could vary between 5 and hundreds.

    My intention is to create a new row on the each time it goes through the For loop (which I will eventially convert to a 'do while' once this is working).
    The code I provided will add a row each time but what your code is doing is clearing cells because of CreateCells method.

    I would suggest trying YourDataGridViewName.Rows.Add and if you have a great deal of rows or not use a try/finally as shown below

    Code:
    DataGridView1.SuspendLayout()
    Try
        ' add rows
    Finally
        DataGridView1.ResumeLayout()
    End Try

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.