Tuesday, September 21, 2010

Accessing Grid view Data : ASP.Net

Scope of accessing data in a Gridview can be bifurcated into three major parts:
1. When Gridview is not in Edit mode
2. When Gridview is not in Edit mode and column is changed to Template
3. When Gridview is in Edit mode


When Gridview is not in Edit mode then the data is representation inside Gridview is as good as writing a text betting tags of a Table i.e. data is in cell.
Sample code:
GridViewName.Rows[row.RowIndex].Cells[integer value]).Text
Note: Here, ‘integer value’ is the Column Number which can be hard coded like 1,2,10, etc.


When Gridview is not in Edit mode and column is changed to Template then cell property cannot be used to access/extract data.
Note: If you make a particular column as Template then Cell property won’t work since Label controls are used to show data during non-Editable mode.
Sample code:
((Label)(GridViewName.Rows[i].FindControl("lblItem"))).Text
Note: Here, ‘i’ is the RowIndex which can be captured using method explained in 1st section.

When data is in Edit mode then all column data values are shown using different controls.
Sample Code:
((DropDownList)(GridViewName.Rows[i].FindControl("cmbName"))).SelectedValue
Note: You can use any properties of the control after using above method. You can also use column index after specifying row index instead of using FindControl to find that control in that row.

No comments:

Post a Comment