hazz

Given the creation of a DataGridView contents

With Me.DataGridView1.Rows
For i = 1 To nRows
.Add(i)
.Item(i - 1).Cells.Item(0).Value = i
.Item(i - 1).Cells.Item(1).Value = sChannelName.ToString
next i
End with

I am trying to insert a row at any point in the grid and move any subsequent rows up by one.

Sub InsertRow(ByVal iRowPosition As Int16)
With Me.DataGridView1.Rows
.Insert(iRowPosition)
strName = "FX1"
.Item(iRowPosition).Cells.Item(0).Value = iRowPosition
.Item(iRowPosition).Cells.Item(1).Value = strName
End With
End Sub

Although this does insert a row with a FX1 in the correct row, how do I increment all the other rows after the one inserted
Do I have to do a resort or a refresh as well

Thank you,
-greg



Re: Visual Basic General incrementing rows after the one inserted in a DataGridview

spotty

Just write a recalculate function to recalculate the values in the first column.

Simple example application which has a recalculate method

Public Class Form1
Dim nrows As Integer = 10

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer

With Me.DataGridView1.Rows
For i = 1 To nrows
.Add(i)
.Item(i - 1).Cells.Item(0).Value = i
.Item(i - 1).Cells.Item(1).Value = "foo"
Next i
End With
End Sub

Private Sub InsertRecord(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
InsertRow(6, "Should be Inserted at Item 6")
Recalculate()
End Sub

Sub InsertRow(ByVal iRowPosition As Integer, ByVal Strname As String)
With Me.DataGridView1.Rows
.Insert(iRowPosition)
.Item(iRowPosition).Cells.Item(0).Value = iRowPosition
.Item(iRowPosition).Cells.Item(1).Value = strName
End With
End Sub

Sub Recalculate()
Dim icounter As Integer = 1
For icounter = 1 To nrows
Me.DataGridView1.Rows(icounter).Cells.Item(0).Value = icounter
Next
End Sub
End Class





Re: Visual Basic General incrementing rows after the one inserted in a DataGridview

hazz

Thank you!

I came up with this below but I like what you did. -greg

For i = iRowPosition + 1 To nRows

With Me.DataGridView1.Rows

.Item(i).Cells.Item(0).Value = i + 1

End With

Next i





Re: Visual Basic General incrementing rows after the one inserted in a DataGridview

dotnetsr

Dear Friend,

I Have VS2003 and don't have DataGridView. I have DataGrid only. I have same problem with item Number column. So How can I use this code to solve my problem in VS2003. Can any one help me please

Your help is most appreciated.

Thanks in advance





Re: Visual Basic General incrementing rows after the one inserted in a DataGridview

dotnetsr

Thanks it is solved now