Click here to Skip to main content
13,507,789 members
Rate this:
Please Sign up or sign in to vote.
hi friends

How to View my Generic List to a label in a webpage when a button is fired from another page .. or else in gridview

        string itemName;
        int count = GridView1.Rows.Count;
        List<int32> id = new List<int32>();

        foreach (GridViewRow item in GridView1.Rows)
            itemName = item.Cells[1].Text;
            id.Add(method to get id from database);

foreach (Int32 txt in id)

            globalvariable+= txt.ToString();

please provide some code
Posted 17-Dec-12 18:54pm
Updated 17-Dec-12 22:49pm
jibesh 18-Dec-12 1:11am
how you want to display ? a list is nothing but the collection of objects just iterate through the items and add formatted text to label or gridview or any other control as you like.
sreeCoderMan 18-Dec-12 1:16am
i want to display a label in gridview
jibesh 18-Dec-12 1:25am
Please elaborate your question. Also hope you know about what is Generic List? how to iterate the list?

are you using any database?
You may use the 'Improve Question' link at the right bottom of your question of you want to edit your question in future.
sreeCoderMan 18-Dec-12 1:29am
i have updated the question i this this may help you to view my list to another page from a button click event to display it in label inside gridview
jibesh 18-Dec-12 3:07am
Check my solution and reply is that your trying or not
Zaf Khan 18-Dec-12 23:03pm
to display the data in thelabel (the data you collect from all the rows) simply assign the concatenated resulting string in the label using the relavent property.

if the label is server side label then use label.text property if its clientside label then use label.innerhtml property.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

In your c# i'm not sure how to code it,
but its not difficult.

So if i provide it in VB maybe you will understand hopefully?


for each (GridViewRow item in GridView1.Rows)

 itemName = item.Cells[1].Text;
 id.Add(method to get id from database);

In VB 

For each (GridViewRow item in GridView1.Rows)
  itemName = item.Cells[1].Text
  id.Add(method to get id from database)
  'comment add the data ietem to a string
  strData = strData & itemName & vbcrlf 
  'Comment vbcrlf is same as chr(13) or enter or newline
SomeLabel.Text = strData
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

string labelTextData = string.Empty;
int count = GridView1.Rows.Count;
List id = new List();

foreach (GridViewRow item in GridView1.Rows)
 labelTextData += item.Cells[1].Text + ",";
 labelTextData += (method to get Id from Database);
label.Text = labelTextData;

is that what you want? for this you dont need the list.
sreeCoderMan 18-Dec-12 4:16am
here only one Text data will display right... i need all rows data lets say like this a,b,c for 3 rows
jibesh 18-Dec-12 4:19am
it will add the result of all the rows give a try and see the result
sreeCoderMan 18-Dec-12 4:42am
yeah it will add the result... but displays the last added value only
jibesh 18-Dec-12 4:45am
"it will add the result" is that what you want??

"displays the last added value only"
did you check the += operator . double check your code. or please update your question with latest code.

Use Improve Question link at the right bottom of your question to modify the question
sreeCoderMan 18-Dec-12 4:47am
i need a comma in between this how to do that
jibesh 18-Dec-12 4:50am
solution updated please refer the solution .

add ',' before adding the string. you can do it urself!! I bet.
jibesh 18-Dec-12 4:54am
I saw your updated code. why do you need two looping? its not a good practice. there was a mistake in my previous code I forget to add += for the first assignment please check the updated solution it will work for you fir sure.
sreeCoderMan 18-Dec-12 4:58am
use of unassigned local variable itemName
jibesh 18-Dec-12 5:00am
OMG.. you are totally lost :) take a break kid!!
sreeCoderMan 18-Dec-12 4:59am
i have edited my code to your code it shows unasigned local variable itemName
jibesh 18-Dec-12 5:03am
am sorry.. Please use my code.. it wasnt my code in u r sample. I still see two loops :)

use string itemName = string.Empty; in C# you cannot leave any variable unassigned if such error comes check the variable definition and set its value to null or any default value matches to the condition. in this string.Empty will work for you.
sreeCoderMan 18-Dec-12 5:06am
labelTextData in your code, i am using itemName in which i am getting id on the basis of itemName from database my argument was itemName
jibesh 18-Dec-12 5:08am
string itemName = string.Empty; to remove unassigned local variable compiler error
sreeCoderMan 18-Dec-12 5:13am
can u give your mail id
sreeCoderMan 18-Dec-12 5:35am
to get id from my database i use columns text to get the id from database so i cant use ","
jibesh 18-Dec-12 5:43am
why dont u try defining a local variable and try all the formatting. think all the possible case before you start writing the code. write pseudo code (means general formulas not the real code) and try all possible cases then start using the visual studio.
jibesh 20-Dec-12 7:06am
any luck ??

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 |
Web04 | 2.8.180417.1 | Last Updated 20 Dec 2012
Copyright © CodeProject, 1999-2018
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