Advanced usage of Asp.net girdview

Source: Internet
Author: User

Today we will discuss the further usage of the gridview:

    1. Get cell data through row and column

If we need to obtain the data of a cell in the gridview control, we can use the following ideas. First, in the selectedindexchanging event of the gridview control, use int Index = E. newselectedindex obtains the index of the selected row, and then uses gvshow. rows [Index]. cells [0]. text; get the value of the column corresponding to the selected row.

    1. Two important properties of gridview

Attribute name

Description

Datakeynames

An array used to obtain or specify the names of primary key fields in the gridview. Multiple primary key fields are separated by commas.

Datakeys

Obtains the object set of each primary key value set by datakeynames in the gridview.

UseCodeBind primary key

Gridview Control ID. datakeynames = new string [] {

"Primary key field name 1", "primary key field name 2 ",....

}

After we set the primary key value for the gridview, we can obtain it in a certain way!

01. How to obtain a single primary key field:

Gridview Control ID. datakeys [Index]. value. tostring ();

02. How to obtain multiple primary key fields

Gridview Control ID. datakeys [Index] ["primary key field name"]. tostring ();

Or

Gridview Control ID. datakeys [Index]. Values ["primary key field name 1"]. tostring ();

Functions of datakeys:

We can use the datakeys value to transfer values between pages.

In the selectedindexchanging event of the gridview, we can use the following code to obtain the primary key of the selected row and pass the primary key value to other pages:

 
1Int Index =E. newselectedindex;2 3String key =This. Gvhr. datakeys [Index]. value. tostring ();4 5Response. Redirect ("modify. aspx? Id = "+ key );
    1. Event Processing Mechanism in gridview

Rowediting event:

Attribute name

Description

Neweditindex

Obtains or sets the index of the edited row.

Cancel

Set the cancel attribute to true to cancel the edit operation.

Note: If the editindex of the gridview is set to-1, no row is edited.

Code for setting and editing row indexes:

In the rowediting event of the gridview, write the following code:

// Set and edit the row Index

This. gvhr. editindex = E. neweditindex;

// Because the page is sent back, the page cannot save the object status and needs to be re-bound

Mydbbind ();

When we click the edit button, the page will automatically display

Click the update and cancel buttons. If you click Cancel, the following exception message is displayed,

 

In this case, you can enter the following code in the rowcancelingedit event of the gridview:

Gvhr. editindex =-1;

Loaddata.

When you click the update button, the following prompt is displayed:

 

At this time, we need to write the code in the rowupdating event:

Rowupdating event

We can write the following code in the rowupdating event:

 1     Int Index = This  . Gvhr. editindex;  2   3               String Userid = Gvhr. datakeys [Index]. value. tostring ();  4   5              String Username = (gvhr. Rows [Index]. findcontrol ( "  Txtname  " ) As  Textbox). text;  6   7               String Gender = (gvhr. Rows [Index]. findcontrol ( "  Ddlgender  " ) As  Dropdownlist). selectedvalue;  8  9               String Phone = (gvhr. Rows [Index]. findcontrol ( "  Txttelephone  " ) As  Textbox). text;  10   11               String Post = (gvhr. Rows [Index]. findcontrol ( "  Ddlpost  " ) As  Dropdownlist). selectedvalue; 12   13               String Address = (gvhr. Rows [Index]. findcontrol ( "  Txtaddress  " ) As  Textbox). text;  14   15   Updatedata (userid, username, gender, phone, post, address );  16   17 Page. clientscript. registerclientscriptblock ( This . GetType (),  18   19       "  Info  " , "  <SCRIPT> alert ('Update successful |! ') </SCRIPT>  "  );  20   21    22   23 Gvhr. editindex =- 1  ; 24   25 Loaddata ();

 

In this way, data is updated and canceled in the editing status. In fact, as long as we have mastered the gridview event mechanism and can write code in the corresponding event, no exception will occur.

Of course, there are still a lot of gridview events. Here we just illustrate several typical events and will continue to update the content of the gridview in the future.

 

 

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.