Access Gridview Columns by Row.Cells

Hai I use the following code to access the columns in a row, but always display an empty row when using Row.Cells[1].Text I use this code in the GridView Unload event handler.

Thanks in advance.

 foreach (GridViewRow row in grvSearchRingTone.Rows) { String coltext = row.Cells[1].Text; } 
+4
source share
2 answers

This can be done in the Gridviews RowDataBound Event, i.e.

  protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { String coltext = e.Row.Cells[1].Text } else if(e.Row.RowType == DataControlRowType.DataRow) { String coltext = e.Row.Cells[1].Text } } 

Hope this helps.

+1
source

I would suggest using the Gridview Databound event . Because the

The unload event occurs when a server control is unloaded from memory.

 protected void GridView1_DataBound(object sender, EventArgs e) { foreach (GridViewRow row in grvSearchRingTone.Rows) { String coltext = row.Cells[1].Text; } } 

Data binding events occur after the server control communicates with the data source.

To see how gridview events work, check out MSDN.

+5
source

All Articles