dcsimg
Results 1 to 8 of 8

Thread: Number all rows in datagriodview

  1. #1

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,189

    Number all rows in datagriodview

    Hi,

    I am a bit puzzled here...

    I have a DGV with two columns (there is more but I removed it)

    Name:  1.JPG
Views: 157
Size:  9.8 KB


    So I want to press a button and number all the rows, starting from number 1 down to the last row.

    Code:
    Try
                For Each row As DataGridViewRow In TblAssessmentDataGridView.Rows
                    row.Cells(0).Value = row.Index + 1
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    So this is telling me that each row will be set to the index number (+1) because I don't want to start numbering a 0. It is also telling me that this above code is for column 0 (the first column)

    I press the button once:

    Name:  2.JPG
Views: 154
Size:  9.8 KB

    Now there is two problems.


    1 - Everything did not re-number it is supposed to be 1,2,3,4,5,6 I don't even understand how it got to 1,1,1,2,4,6.....
    2 - I only want to re-number first column but now 2nd column is .... shuffled?

    Press button again............

    Name:  3.JPG
Views: 154
Size:  10.5 KB

    Same two problems...

    Press button again............

    Name:  4.JPG
Views: 154
Size:  9.6 KB

    Now everything is numbered, but the 2nd column is still wrong...

    I have ORDER BY on the number (in the tableAdapter query) I actually also tried to remove the ORDER BY but it is still doing the same thing.

    How can I re-number ONLY the first column with one click?

    Thanks...

    PS. The DGV is bound and I have ORDER BY 'No' in the the TableAdapter Query (not sure if this is the problem..)
    Last edited by schoemr; Sep 25th, 2019 at 09:21 AM.
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  2. #2
    Addicted Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    133

    Re: Number all rows in datagriodview

    I have never used data binding, but I recall from your previous posts JM saying changes should be made against the source data NOT the UI display control, which is then reflected automatically because of that binding. If your first column is bound then it will be reflecting what is in the source data and not what you change in the grid.

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,445

    Re: Number all rows in datagriodview

    My suggestion is don't use the rowindex, this may be part of how the rows are organized. Instead,

    For rowCounter = 0 to yourDataTable.Rows.Count - 1
    yourDataTable.Rows(rowCounter).Item("yourFieldName") = rowCounter + 1
    Last edited by wes4dbt; Sep 25th, 2019 at 04:02 PM.

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,523

    Re: Number all rows in datagriodview

    Try using ROW_NUMBER if SQL-Server

    Code:
    SELECT ROW_NUMBER() OVER(ORDER BY dbo.Contacts.LastName) RowIndex,
           ContactId,
           FirstName,
           LastName
    FROM dbo.Contacts;

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,706

    Re: Number all rows in datagriodview

    With regards to the original problem, have you set the Sort property of the BindingSource? If you have then the order of the rows would likely change after each iteration of the loop, which would change the Index of the rows each iteration, which would account for the duplicate values in that column. Assuming you need to stick to the original plan, the solution is to create an array containing the rows and loop through that, using the array index rather than the grid index. Sorting in the grid won't change the order in the array. You should use a For loop rather than a For Each loop and use the loop counter as the basis for the row number.

  6. #6

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,189

    Re: Number all rows in datagriodview

    Quote Originally Posted by jmcilhinney View Post
    With regards to the original problem, have you set the Sort property of the BindingSource? If you have then the order of the rows would likely change after each iteration of the loop, which would change the Index of the rows each iteration, which would account for the duplicate values in that column. Assuming you need to stick to the original plan, the solution is to create an array containing the rows and loop through that, using the array index rather than the grid index. Sorting in the grid won't change the order in the array. You should use a For loop rather than a For Each loop and use the loop counter as the basis for the row number.
    Hi, no I did not set the sort property of the BindingSource. I added a SORT BY in the SELECT query of the TableAdapter...(I also removed it to test if it shall make a difference which did not)

    About this array; you say it should contain the 'rows'... All the rows or just the rows of that particular column? Then you say I should loop through the array with a FOR.. and then do what?
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  7. #7
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: Number all rows in datagriodview

    Quote Originally Posted by schoemr View Post
    Hi, no I did not set the sort property of the BindingSource.
    The behaviour you are describing in Post#1 is the result of Column(0) having been set to sort ascending. Have you clicked Column(0)'s header cell? Is there as sorting glyph in that header cell? (You might need to increase the width of the column to be able to see it). If so, the binding source will also have been set to sort on that column.

    You can test by checking the BindingSource's IsSorted property, and the following should fix your problem:
    Code:
    yourBindingSource.Sort = Nothing
    
    For Each row As DataGridViewRow In TblAssessmentDataGridView.Rows
        row.Cells(0).Value = row.Index + 1
    Next

  8. #8

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,189

    Re: Number all rows in datagriodview

    Quote Originally Posted by Inferrd View Post
    The behaviour you are describing in Post#1 is the result of Column(0) having been set to sort ascending. Have you clicked Column(0)'s header cell? Is there as sorting glyph in that header cell? (You might need to increase the width of the column to be able to see it). If so, the binding source will also have been set to sort on that column.

    You can test by checking the BindingSource's IsSorted property, and the following should fix your problem:
    Code:
    yourBindingSource.Sort = Nothing
    
    For Each row As DataGridViewRow In TblAssessmentDataGridView.Rows
        row.Cells(0).Value = row.Index + 1
    Next
    Thank you!
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

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