Sunny
Sunny

Reputation: 89

How to fetch cell values from a grid view and store it in a variable

i want to fetch cell values of a single row displayed in a grid view. This screen shot gives an idea of the problem.

Grid view Showing details of a single person

i want to store the name, mobile, email and course_interested in different variable here. any help is much appreciated.

Upvotes: 1

Views: 9244

Answers (2)

Wasif Hossain
Wasif Hossain

Reputation: 3950

If the name of your gridview is gridview1 (say), then you may do the following:

var name = gridview1.Rows[0].Cells[0].Text;
var mobile = gridview1.Rows[0].Cells[1].Text;
var email = gridview1.Rows[0].Cells[2].Text;
var courses_interested = gridview1.Rows[0].Cells[3].Text;

EDIT:

If the databound item is System.Data.DataTable, then use the following:

var name = dt.Rows[0][0].ToString();
var mobile = dt.Rows[0][1].ToString();
var email = dt.Rows[0][2].ToString();
var courses_interested = dt.Rows[0][3].ToString();

Upvotes: 3

Francis
Francis

Reputation: 94

You need to get each strings for each cells included in the single row. Once you have aqcuired those strings you can now assign a variable for each of them. Here's how you can access the values once you have placed them in a variable:

    variable x = gridview.Rows[0]["ColumnNameOrIndex"];

Hope this was helpful!

Upvotes: 1

Related Questions