Click here to Skip to main content
15,896,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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

1 solution

Try something like this:

VB
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
    else
        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.
 
Share this answer
 
Comments
12345_abcde 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 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 9-Jul-13 3:10am    
sorry, i dont quite understand what you mean.
Pheonyx 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:

http://stackoverflow.com/questions/3530036/grouping-datagridview-in-c-sharp-winforms

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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900