Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
btn_Click()

For Each row As DatagridviewRow In DGV.Rows
    row.Cells("Column2").Value = "Boy "
    row.Cells(Column2").Value = "Girl"
    row.Cells(Column2").Value = "Boy"
    row.Cells(Column2").Value = "Girl"
    row.Cells(Column2").Value = "Girl"
Next


This is the code but when I execute the code it inserts only the first value which is "Boy" throughout. But I want to insert different values
Posted
Updated 26-Jan-16 9:52am
v2

The code you posted won't even compile as you have string-based identifiers with mismatch quote marks.

Removing those, yeah, your code would put "Boy" in every row because that's what you're telling the code to do. You're not changing to different values in each row. You have no code that is saying anything about any other values.

If you want to put a different value in each row, you either have to have code inside the loop that determines which value to place in the row pointed to by the loop OR you need to remove the loop and explicitly put a line of code for each row in the DGV to set its value in the appropriate column for that row.
 
Share this answer
 
Comments
Richmond Boateng 26-Jan-16 16:14pm    
Please, how do I place a code inside the loop. When I put a line of code for each row, In my software, at some point, I would have 10 or 20 rows and I might create the code for 30 rows which can't be possible.
Dave Kreskowiak 26-Jan-16 16:59pm    
You're either going to do this:

DGV.Rows(0).Cells("Column2").Value = "Boy"
DGV.Rows(1).Cells("Column2").Value = "Girl"
DGV.Rows(2).Cells("Column2").Value = "Girl"
DGV.Rows(3).Cells("Column2").Value = "Boy"

...

or

For Each row As DatagridviewRow In DGV.Rows
' Code to determine which value you're sticking
' in the row pointed to by 'row' goes here

row.Cells("Column2").Value = value
Next

What you put in place of the "Code to determine..." comment is entirely up to you. I have no idea what you're trying to do so it's impossible for me to supply any code for that part.
Richmond Boateng 26-Jan-16 19:46pm    
Thank you very much.
The fact is am creating a school management system. And this aspect is a position system. Where I've already displayed sum total of all marks of student in descending order from the database. So at the next column, I wanted to give them position like 1st,2nd,third in that order.
Meaning the one with the highest total marks will get "1st" in the next column of the same row.
That's it
it looks your code will not work as expected, cause you have throw loop on each row with specific cell, it will result in only one value set in all cell. you need to change your code as follows
VB
DGV.Rows(0).Cells(2).Value = "Boy"
DGV.Rows(1).Cells(2).Value = "Girl"
DGV.Rows(2).Cells(2).Value = "Boy"
DGV.Rows(3).Cells(2).Value = "Girl"
DGV.Rows(4).Cells(2).Value = "Girl"
 
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