Jack
Jack

Reputation: 23

How to get values from a templateFiled in GridView in c#

I am working on an assignment and I have a GridView that uses an SQL Datasourse.

The GridView has 2 columns, 1. name and 2. LastNmae. now I want to add one more column

which this column does not exist in the database. What I did is add a TemplateField

and I edited it and added DropDownList for the user to select an age.

The question is how can I get the value from what the user chooses from the DropDownList?

for exaple, if I want to get what is the first name, I would do this

GridView1.SelectedRow.Cells[0].Text;

how can I do for the DropDownList? Please help, thanks!

Upvotes: 0

Views: 79

Answers (1)

Manish Sharma
Manish Sharma

Reputation: 2426

Use this

DropDownList dll = (DropDownList)((Control)e.CommandSource).NamingContainer.FindControl("ddPStatud");
String selectedValue = dll.SelectedValue;

and Use also.

string city = (gvCustomers.Rows[e.RowIndex].FindControl("ddlCities") as DropDownList).SelectedItem.Value;

Upvotes: 1

Related Questions