Click here to Skip to main content
11,806,818 members (57,055 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: VB VB.NET
i have datagridview with 2 columns, headings: ModuleCode, AdminNo(which is students number)
i want to populate:
EG1001 s1
-blank- s2
-blank- s3

EG1002 S2
-blank- S4

with my current code:

Dim j As Integer
       For i = 1 To dgvModStud.Rows.Count - 1
           If dgvModStud.Rows(i).Cells(0).Value = dgvModStud.Rows(j).Cells(0).Value Then
               dgvModStud.Rows(i).Cells(0).Value = String.Empty
           End If
       Next i

only my first modulecode is applied. but for the others it doesnt.
i guess something wrong with my j.
how can i get it incrementing?
Thanks in advance!
Posted 7-Jul-13 22:54pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try something like this:

Dim currentModuleCode As String = String.Empty
For i = 1 To dgvModStud.Rows.Count - 1
    If dgvModStud.Rows(i).Cells(0).Value = currentModuleCode Then
        dgvModStud.Rows(i).Cells(0).Value = String.Empty
        currentModuleCode =  dgvModStud.Rows(i).Cells(0).Value
    End If
Next i

Then you do not need to worry about increment j. Also it means that when the module code changes you will get an entry for it, but the rest for that code should be blank.

This also means that if the list is not ordered then if a code re-occurs it will have a its code visible so it does not appear as if it is under the wrong one.
12345_abcde at 8-Jul-13 21:34pm
Hi thanks for you help! it worked. but why is it that when i click on the datagrid header for sorting, then the thing sort of messed up.

btw i changed it to i=0 if not my first modulecode appears twice (:
Pheonyx at 9-Jul-13 3:01am
It will mess up because you have just cleared the values you are trying to sort on.

I would suggest you look at the possibility of grouping and see if that better suites what you are trying to do. As you can declare a group header and then not display the same field again. without this sort of code. It is not something I have done much with so I can't advise to much on that though.

If the solution worked for you please could you mark it as accepted, thanks.
12345_abcde at 9-Jul-13 3:10am
sorry, i dont quite understand what you mean.
Pheonyx at 9-Jul-13 3:17am
Basically, the code you have requested so far clears the values in cells. When you click on the column to sort it, there are alot of blank values to sort against (i.e. it does not know the original value).
I have just had a quick google, and my initial thought that there is built in grouping ability in the .net controls was mistaken. However this link here:

talks about how it can be implemented with custom code, or with third party controls.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 ppolymorphe 245
1 OriginalGriff 150
2 George Jonsson 145
3 CPallini 100
4 Abhinav S 80
0 OriginalGriff 3,380
1 Maciej Los 1,990
2 CPallini 1,915
3 KrunalRohit 1,907
4 ppolymorphe 1,317

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 8 Jul 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100