Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 10 labels and 10 textboxes in my form . i am trying to set the text of the labels and textbox using the datagridview coloumns i have tried the code below but it is setting the text only for the first label and not the rest..please guide mr through this guyz..
For Each row As DataGridViewRow In FormulativeDV.Rows
    If Not row.IsNewRow Then
        Lbl1.Text = (row.Cells(0).Value.ToString)
    End If
Next
Posted
Comments
DinoRondelly 13-Dec-12 10:57am    
Are all your labels named Lbl1?

Thats your problem ...
Pratik65 13-Dec-12 11:18am    
no its lbl2,lbl3..till 10 so what should i do so that i can set the text as per the datagrid view columns
DinoRondelly 13-Dec-12 11:24am    
And this my friend is programming, your going to have to come up with the logic in your loop.

This should be fairly simple if you just play with it for awhile and try some things.
DinoRondelly 13-Dec-12 11:26am    
And also lbl1 through lbl10 is a horrible naming convention, everything should have meaningful name.

Example lbl_FirstName
ZurdoDev 13-Dec-12 11:17am    
As pointed out, it only updates label 1 because that is what you told it to do. You need to have Lbl2.Text = whatever and Lbl3.Text = whatever. I would put them in a loop.

1 solution

You have at some point in your code selected the Label from the first row in your grid. You simply need to grab the control from each row before you set the value like this:
VB
For Each row As DataGridViewRow In FormulativeDV.Rows
   If Not row.IsNewRow Then
      Label lblCtl = (Label)row.FindControl("LabelName")
      If Not lblCtl = Nothing Then
         lblCtl.Text = (row.Cells(0).Value.ToString)
      End If
   End If
Next
 
Share this answer
 
Comments
Pratik65 13-Dec-12 13:40pm    
but i have 10 labels and how do i loop through a label and set thei text ?

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