Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 23:54pm

1 solution

Rate this: bad
good
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
    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.
  Permalink  
Comments
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:
 
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)

  Print Answers RSS
0 OriginalGriff 539
1 Maciej Los 300
2 DamithSL 233
3 Sergey Alexandrovich Kryukov 209
4 BillWoodruff 200
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 8 Jul 2013
Copyright © CodeProject, 1999-2014
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