How to obtain the selected row in the gridview

Source: Internet
Author: User
Basically, no one on the. NET platform has ever used the gridview. It is also a common problem for new users to obtain the selected rows. Now, three possible cases are summarized based on the previous code.

Note that the three methods here are not complementary to each other, but applicable in different situations.

I. The page Code contains the following statements:

Onselectedindexchanged = "gridview2_selectedindexchanged" (in fact, double-click the gridview in the design view to automatically generate this statement, and automatically generate the following statement in C)

Protected void gridview1_selectedindexchanged (Object sender, eventargs E)

{

Int Index = gridview1.selectedindex; // Index Number of the selected row

Try

{

If (index! =-1)

{

Textbox2.text = This. gridview1.selectedrow. cells [1]. text. tostring (); // the elements in the second column of the selected row are obtained here. Note that the column here must be a boundfield column and cannot be a templatefield. If it is the latter, use findcontrol, the second method has examples.

}

In this way, the gridview usually has a selection button (which can be edit, delete, or another button in templatefield). In short, the button needs to drive the change of the selected row.

II. sometimes a button may not be selected in the gridview, but a field is used as a button (in fact, it is still a button, but the text value of the button is bound to the field ), generally, the field can be the index key or name in the database. Note that this must be the templatefield column.

For example, the following page code binds the ID field to the button. Our goal is to obtain the value of this (ID) When you click this button.
 

<Asp: linkbutton id = "linkbutton1" oncommand = "linkbutton#clicked" runat = "server" text = '<% # BIND ("ID") %>'> </ASP: linkbutton>

C # The Code is as follows:

Public void linkbutton1_clicked (Object sender, commandeventargs E)

{

Int Index = int. parse (E. commandargument. tostring ());

String id = (linkbutton) gridview1.rows [Index]. cells [0]. findcontrol ("linkbutton1"). text;

III. in more general cases, you only need to add the following statement to the page code's gridview code, whether the button is built-in selection, editing, or custom button in templatefield:

Onrowcommand = "gridview2_rowcommand"

Then the Page code is:

Protected void gridview2_rowcommand (Object sender, gridviewcommandeventargs E)

{

Control C = (Control) E. commandsource );

Gridviewrow gvr = (gridviewrow) C. Parent. parent; // here gvr is the selected row. The following statements apply to boundfield and templatefield respectively.

String S = gvr. cells [0]. text;

// Or

String S = (Label) gvr. findcontrol ("label1"). text;
 

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.