Home>

I started touching VB.NET from November.

The data brought from DB is displayed in DataGridView.
I want to set the value in the specific cell of the previous row to the specific cell of the newly added row when I add a new row there.

Example)

ID NAME CLASS
0001 NIC 1J
0002 EDGAR 1J
0003 GEORGE 2J
0004 ALFRED 3J
3J

The last line to be added.
At this time, I want to set the CLASS cell "J3" in the previous row to the same cell in the newly added row.
To add a row, prepare a "+ Add row" button on the screen so that a new row is added each time you click it.

Current progress Implemented up to the point where a new line is added when the

button is pressed.

Applicable source code
Private Sub NewRowsButton_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewRowsButton.Click
        Dim dr As DataRow = ClassList.NewRow
        ClassList.Rows.Add (dr)
    End Sub
Private Sub NewRowsButton_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewRowsButton.Click
        Dim dr As DataRow = ClassList.NewRow
        dr ("CLASS") = "J3"
        ClassList.Rows.Add (dr)
    End Sub

If i do the above, "J3" will appear in the CLASS cell when adding a row, but of course the previous row might not always be "J3", so it was no good.

Additional questions

When deleting a newly added line,

Private Sub DeleteRowsButton_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteowsButton.Click
        Try
            'DataGridView = mrs
            Dim ClassData As DataTable = CType (mrs.DataSource, DataTable)
            For i As Integer = 0 To ClassData.Rows.Count-1
                'celCHECK is a selection box. If not selected and "0" is selected, it will change to "1".
                If ConvertInt32 (ClassData.Rows (i) ("CHECK")) = 0 Then Continue For
                ClassData.Rows.RemoveAt (i)
            Next
        Catch ex As Exception
            m_objLogFunc.WriteExceptionLog (ex)
        Finally
            'Force return to screen
            getMenuBar ()
        End Try
    End Sub

I have a deletion process like this.
For example, when there are 5 rows of data, when you delete the 3rd row (Rows (2)), it will be hit by saying that there is no row in the 5th row (Rows (4)).
I wondered if I could put "i-= 1" after deletion, but it didn't work. Along with the addition of new lines, we would appreciate any good advice.

  • Answer # 1

    If you want to get the value of the previous last line quickly, is it like this?
    (If ClassList is empty, take appropriate measures.)

    Dim drLast As DataRow = ClassList.Rows (ClassList.Rows.Count-1))
        Dim className As String = drLast ("CLASS")
        Dim dr As DataRow = ClassList.NewRow
        dr ("CLASS") = className
        ClassList.Rows.Add (dr)
    Answer to additional questions

    I think it's better to have a thread as another question, but here are some additional questions to answer.
    For i = 0 It is not recommended to delete rows such as ClassData.Rows.RemoveAt (i) in To ... to Next.
    If ClassData.Rows.RemoveAt (i) is performed in the loop, the index will be shifted by the amount of the removed DataRow.
    This may cause the DataRow in Rows (i) to be a different DataRow than intended.
    Also, if you define "For i As Integer = 0 To ClassData.Rows.Count-1" in the loop, even if the value of ClassData.Rows.Count changes during the loop, the upper limit of i will be I think it will be fixed.
    I think this is the reason for the "no rows in Rows (4)" error.

    There are various countermeasures, but if you want to use the current code,
    Instead of "For i As Integer = 0 To ClassData.Rows.Count-1",
    "For i As Integer = ClassData.Rows.Count-1 To 0 Step -1"
    Is to evaluate the line from the opposite direction.
    If this is the case, you don't have to worry about misalignment caused by missing DataRow.

Trends