简体   繁体   中英

Can't get row values from GridView

This is the loop I'm using. I get the correct number of rows and columns back, so I'm assuming I'm binding correctly.

However Text is always empty. Why?

for (int i = 0; i < GridView1.Rows.Count; i++)
{
    for (int k = 0; k < GridView1.Columns.Count; k++)
    {
        string cellText = GridView1.Rows[i].Cells[k].Text;
    }
}

I have found a few pointers but they don't help.

Edit 1

This is my GridView with one record

在此处输入图片说明

The only empty value is under Responsibility.

From Ehsan Sajjad's answer below, I understand that my code would always get the last value, ie "Classics".

Is that correct?

If it is, then why am I getting empty Text for all columns?

Edit 2

I've solved it myself. Thanks to those who helped in this thread. See my own answer below.

Easisest way is to use :

foreach(GridViewRow row in GridView1.Rows)
{
for(int i = 0; i < GridView1.Columns.Count, i++)
{

    String cellText = row.Cells[i].Text;
}
}

Try this

string cellText = "";

for (int i = 0; i < GridView1.Rows.Count; i++)
{
    for (int k = 0; k < GridView1.Rows[i].Cells.Count; k++)
    {
        cellText += GridView1.Rows[i].Cells[k].Text;
    }
}

My gridview was using DynamicFields .

The solution was simply to replace all my DynamicFields with standard BoundFields and BOOM all the data was there.

Next time I won't be using DynamicData framework, that's for sure.

On this other thread you can find the complete scenario of my original problem.

your answer lies in the link which you mentioned in you question, your cells might have controls inside it try accessing it with the help of row.Cells[1].Controls collection. lastly set a break point in loop and check are you able to get into the second loop ? i tried loading gridview with the help of SqlDataSource and tried to access value like you did and i were not able to get into the second loop. perhaps this link might help you. How to get the value of a cell specifies the GridView ASP.NET

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM