Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What I want to achieved.

IN MY DGV

COL1	COL2	COL3
ITEM1	1	      1
ITEM2	2	      1
ITEM3	3	      2
ITEM4	4	      2
ITEM5	5	      3
ITEM6	6	      3
ITEM7	7	      4
ITEM8	8	      4
ITEM9	9	      5
ITEM10	10	      5
ITEM11	11	      6
ITEM12	12	      6
ITEM13	13	      7
ITEM14	14	      7




Is there a way to do this stuff? tia

What I have tried:

VB
DGV.Rows(0).Cells(2).Value =1
DGV.Rows(1).Cells(2).VALUE = 1+ DGV.Rows(0).Cells(2).Value
DGV.Rows(2).Cells(2).VALUE = 1+ DGV.Rows(1).Cells(2).Value
DGV.Rows(3).Cells(2).VALUE = 1+ DGV.Rows(2).Cells(2).Value
DGV.Rows(4).Cells(2).VALUE = 1+ DGV.Rows(3).Cells(2).Value
DGV.Rows(5).Cells(2).VALUE = 1+ DGV.Rows(4).Cells(2).Value
DGV.Rows(6).Cells(2).VALUE = 1+ DGV.Rows(5).Cells(2).Value
DGV.Rows(7).Cells(2).VALUE = 1+ DGV.Rows(6).Cells(2).Value
DGV.Rows(8).Cells(2).VALUE = 1+ DGV.Rows(7).Cells(2).Value
DGV.Rows(9).Cells(2).VALUE = 1+ DGV.Rows(8).Cells(2).Value
DGV.Rows(10).Cells(2).VALUE = 1+ DGV.Rows(9).Cells(2).Value
DGV.Rows(11).Cells(2).VALUE = 1+ DGV.Rows(10).Cells(2).Value
DGV.Rows(12).Cells(2).VALUE = 1+ DGV.Rows(11).Cells(2).Value
DGV.Rows(13).Cells(2).VALUE = 1+ DGV.Rows(12).Cells(2).Value
DGV.Rows(14).Cells(2).VALUE = 1+ DGV.Rows(13).Cells(2).Value
Posted
Updated 4-Apr-22 21:23pm

A better way would be to use a loop:
For i As Integer = 0 To 14
    DGV.Rows(i).Cells(2).Value = i + 1
Next
But ... since indexes are zero based, the column you want to index is probably 1, not 2
 
Share this answer
 
Comments
razrx 5-Apr-22 3:35am    
Hi, I tried using your code and also the code of Richard MacCutchan, but seems that it only solves the col2 problem, what should I do to fix my col3 problem?
OriginalGriff 5-Apr-22 4:00am    
For i As Integer = 0 To 14
    DGV.Rows(i).Cells(2).Value = i + 1
    DGV.Rows(i).Cells(3).Value = (i + 2) \ 2
Next
razrx 5-Apr-22 4:20am    
it works, but why did you use backslash?
i mean, you divide stuff there, but when i change it to slash, it gives me numbers with .5 values.
OriginalGriff 5-Apr-22 4:45am    
"\" is the VB Integer Divide: 4 \ 2 and 5 \ 2 both give 2; 6 \ 2 and 7 \ 2 both give 3; and so on.
Arithmetic Operators - Visual Basic | Microsoft Docs[^]
razrx 5-Apr-22 19:50pm    
Thank you very much for this info, It was a great help.
VB
For row As Integer = 1 To 14
    DGV.Rows(row).Cells(2).Value = row
Next
 
Share this answer
 

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