Looping through gridview in C# always return empty

I had a gridview which was bounded to a datasource (datatable in my case) in code behind in C#. Code was something like this

foreach (GridViewRow Row in GV_Items.Rows)
{
     string str = Row.Cells[0].Text;
}

This was always returning empty (”) no matter which column index I was pointing to. I tried different variations like

GridView.Rows[0].Cell[0].Text;

but it failed too. After pondering on it for quite a while and still not resolved, I decided to loop through the data source itself, in my case a datatable. That actually worked! That code looks like this

dtItems.Rows[i][3].ToInt32()

I am still not sure why it will not read the values from GridView in a loop but at least there is work around.

Advertisements
This entry was posted in Tutorials/Tips and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s