Click here to Skip to main content
12,394,573 members (81,731 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
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 13-Dec-12 4:43am
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
ryanb31 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.
Pratik65 13-Dec-12 11:18am
   
ya but i dont know to put a label into a loop and increment it
Richard MacCutchan 13-Dec-12 11:30am
   

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:
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
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2016
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