Because the dropdowelist is different from the button, the CommandName cannot be specified, so there is no way to capture the index of the row in the normal way. Quite a bit of trouble, later found using the NamingContainer attribute to solve the problem.
Let's take a look at Microsoft's explanation of this attribute:
----------------------------------
Gets a reference to the server control's naming container, which creates a unique namespace to differentiate between server controls with the same Control.id property values.
Each page of the ASP.net Web application contains a hierarchy of controls. This hierarchy has nothing to do with whether the control generates a user-visible UI. The naming container for a given control is the parent control on top of the control in the hierarchy, which implements the INamingContainer interface. The server control that implements this interface creates a unique namespace for the ID property value of its child server control.
Creating a unique namespace for server controls is especially important when data binding is performed against list Web server controls, such as Repeater and DataList server controls. When multiple items in a data source create multiple instances of a server control, and the server control is a child of a repeating control, the naming container ensures that each instance of those child controls has a UniqueID property value that does not conflict. The default naming container for a page is an instance of the page class that is generated when you request it.
You can use this property to determine the naming container where a particular server control resides.
----------------------------------
Especially the last sentence.
The following is a summary of the code for the SelectedIndexChanged event:
Copy Code code as follows:
DropDownList DDL = (DropDownList) sender;
GridViewRow GVR = (gridviewrow) DDL. NamingContainer;
int id = Int. Parse (GRIDVIEW1.DATAKEYS[GVR. Rowindex][0]. ToString ());
int num = Int. Parse (DDL. Text);
The first sentence is used to get the DropDownList control that triggers the event.
The second sentence takes advantage of the control's NamingContainer property to get its container, as well as the GridViewRow object I want.
With this, the rest is the usual use, the problem is solved.