U r s u s
U r s u s

Reputation: 6968

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

enter image description here

The only empty value is under Responsibility.

From Ehsan Sajjad's answer below, I understand that my code would always get the last value, i.e. "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.

Upvotes: 0

Views: 1773

Answers (4)

U r s u s
U r s u s

Reputation: 6968

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.

Upvotes: 1

Nima Derakhshanjan
Nima Derakhshanjan

Reputation: 1404

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;
    }
}

Upvotes: 1

asp.netGhost
asp.netGhost

Reputation: 44

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

Upvotes: -1

Manish Goswami
Manish Goswami

Reputation: 865

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;
}
}

Upvotes: 1

Related Questions