i've been all over the internet to solve this problem, but i've found no solution to my problem.my concept is this.
Based on selected value within a combo box, the datagridview will populate with values from the database.

 For test = 0 To containerdetaildatatable.Rows.Count - 1
                    DataGridView1.Rows(test).Cells(0).Value = containerdetaildatatable.Rows(test).Item("Roundtrip")
                    DataGridView1.Rows(test).Cells(1).Value = containerdetaildatatable.Rows(test).Item("NoTrips")
                    DataGridView1.Rows(test).Cells(2).Value = containerdetaildatatable.Rows(test).Item("Distance")
                    DataGridView1.Rows(test).Cells(3).Value = containerdetaildatatable.Rows(test).Item("TypeofTrucks")
                    DataGridView1.Rows(test).Cells(4).Value = containerdetaildatatable.Rows(test).Item("frm")
                    DataGridView1.Rows(test).Cells(5).Value = containerdetaildatatable.Rows(test).Item("Destination")

but each time i select a value from the combobox, the dgv keeps inserting a blank row. i know it has to do with the .add() part. but how else am i supposed to pass the values. i dont want to bind the data.
now i've tried a 100 ways to remove the blank rows from the dgv once all values are populated, but i keep getting the index out of range error. the latest i tried was

Dim i As Integer = 0
                Dim j As Integer = 1

                For Each row As DataGridViewRow In DataGridView1.Rows

                    If row.Cells(j).Value IsNot Nothing AndAlso row.Cells(j).Value.ToString() <> "" Then
                    End If

still it creates the error. can someone provide me with a solution.Thnx

Recommended Answers

All 2 Replies

Your If statement is convlicting with what you are wanting.

You are checking to see if the row is not empty, and checking to see if string value is not empty.

Then removing it when it satisfies both conditions.

I think what you are looking for is:

If IsNothing(row.Cells(j).Value) = true AND row.Cells(j).Value.ToString = "" Then
End If

Also, from what I see, you are only removing the row at position 0.

Are you incrementing your index, i?

One more thing.

If you are wanting to cycle through the entire row, your code should look somthing like this:

    Dim i As Integer = 0
    Dim mtCell As Integer = 0
    For Each row As DataGridViewRow In DataGridView1.Rows
        For j = 0 To row.Cells.Count
            If IsNothing(row.Cells(j).Value) = True And row.Cells(j).Value.ToString = "" Then
                mtCell += 1
            End If

        If mtCell = row.Cells.Count Then
        End If

        i += 1
        mtCell = 0

I'll focus on you adding the blank records rather than deleting them.
change your code to :

 For test = 0 To containerdetaildatatable.Rows.Count - 1
                    dim i as integer =  DataGridView1.Rows.Add()
                     DataGridView1.Rows(i).Cells(0).Value = containerdetaildatatable.Rows(test).Item("Roundtrip")
                     DataGridView1.Rows(i).Cells(1).Value = containerdetaildatatable.Rows(test).Item("NoTrips")
                     DataGridView1.Rows(i).Cells(2).Value = containerdetaildatatable.Rows(test).Item("Distance")
                     DataGridView1.Rows(i).Cells(3).Value = containerdetaildatatable.Rows(test).Item("TypeofTrucks")
                     DataGridView1.Rows(i).Cells(4).Value = containerdetaildatatable.Rows(test).Item("frm")
                     DataGridView1.Rows(i).Cells(5).Value = containerdetaildatatable.Rows(test).Item("Destination")

So basically get the row you are adding and use that to assign values for it's fields.

commented: Ha! Good catch! Didn't even notice it! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.