Reputation: 6791
I have a gridview where each columns have two template fields. One is ItemTemplate and another is EditItemTemplate. Now in EditItemTemplate it will show some textboxes where i can perform necessary editing. I can easily fetch data from each cell by using the following code-
Dim gvRow As GridViewRow = gvView.Rows(e.RowIndex)
CType(gvRow.FindControl("controlID"), TextBox).Text
But how can i fetch data when the grid view is not in editing mode that is in ItemTemplate. I have tried the following-
Dim rowView As DataRowView = CType(gvRow.DataItem, DataRowView)
Dim something As String = rowView("data_field").ToString()
But its getting the exception object reference is not set to an instance of object. This is probably happening because the above code should be used in a method like RowDataBound when a row is instantiated. But I have to fetch data from some other method. Any idea how to do this.
Again for clarification I want to fetch data when the gridview columns are in ItemTemplate Mode.
Upvotes: 1
Views: 4369
Reputation: 1
Two answers worked fine thank you very much.
I spent more time trying to get the GridView Cell value but useless I could not Know what is the problem. When I read your answer I found that my GridView has template fields so that was the problem.
Upvotes: -1
Reputation: 1483
You may consider accessing the gridview datasource directly. Be careful when doing this if you allow sorting / paging on the grid as the index into your datasource will differ.
Upvotes: 1
Reputation: 5806
I guess you want to read value of grid view column in some command. If you have gvRow
which is your gridview's row then you can use .Cell(index).Text
in order to get value of the column.
If you are using template field and using some controls to show column data, you have to do gvRow.findControl("controlId")
and then cast it to appropriate control object to read data from it.
Happy coding
Upvotes: 1