[ASP. NET development]. net notes during the cainiao period [about GridView]

Source: Internet
Author: User

. Net notes are taken from ASP. NET 2.0 Step 1. Thank you for your guidance.

When a beginner wants to display data, I think the GridView control will be the most commonly used one. This blog post will introduce some usage of the GridView.

Body 1. Set the button event of the GridView

When a button in the GridView is clicked, The RowCommand event is triggered. You can set different commandnames for different buttons to differentiate the buttons that trigger the event.

Eg:

<Asp: ButtonField CommandName = "Del" ButtonType = "Button" Text = "Del" HeaderText = "del"/>

Code in the event:

if(e.CommandName=="Del"){    int iIndex=Convert.ToInt16(e.CommandArgument);    Response.Write(iIndex+1);}

The buttons for triggering events are distinguished according to the command name, and then the row index number for triggering events can be obtained from e. CommandArgument.

 

2. How to obtain the primary key of the selected row

The GridView provides a DataKey set to store the primary keys of each row of data. First, set the DataKeyNames attribute of the GridView to the primary key of the data table. If multiple primary keys exist, separate them with commas, then use this attribute in the code to obtain the primary key: GridView1.DataKey [iIndex];

With the primary key value, it is much easier to delete it.

Note: after the operation is complete, you must immediately re-specify the data source and use the binding method to re-bind the data source.

For the delete operation, a user confirmation dialog box is usually added, which can be written in script language. If we convert this column to a template column, you must manually bind the index number to obtain the selected index number in the background. Use CommandArgument = '<% # Container. dataItemIndex %> 'bind the index number. Well, it should be like this.

 

3. How to obtain non-primary key data of the row in the GridView in RowCommand

(1) Method 1: manually specify the CommandArgument of the control

The principle of this method is to bind data directly to the CommandArgument of the button. The disadvantage is that only one data can be bound. Eg: CommandArgument = '<% # Eval ("UserName") %>'

 

(2) Method 2: Get the value directly from the GridView

The advantage of this method is that it can obtain multiple values. The disadvantage is that the values must be displayed on the GridView. The only parameter we need to know is the index number of the row.

Eg:

Int iIndex = Convert. toInt16 (e. commandArgument); GridViewRow gvr = GridView1.Rows [iIndex]; string sUserName = GridView1.Cells [0]. text; // This is the Hyperlink in the template column. Therefore, use the FindControl method to locate it (Control type) based on the Control ID, convert it to the Hyperlink type, and then obtain its value. Hyperlink is the Child control of the Cell and Cell is the Child control of the DataGridView. String sUserEmail = (GridView1.Cells [1]. FindControl ("HyperLink1") as HyperLink). Text;

 

(3) method 3: Use hidden fields to save values

The advantage is that the data can be any data in the data source, not necessarily the data that needs to be bound to the GridView. The disadvantage is that the data should not be stored too large, otherwise the page size will increase dramatically.

Eg:

<Input type = "hidden" runat = "server" id = "hid_UserName" value = '<% # Eval ("UserName") %>'/>

RowCommand Event code:

int iIndex= Convert.ToInt16(e.CommandArgument);GridViewRow gvr = GridView1.Rows[iIndex];string sUserName = (gvr.FindControl("hid_UserName") as HtmlInputHidden).Value;

 

(4) Method 4: obtain values from the data source again

The advantage of this method is that it is not bound, but the disadvantage is that you need to access the database again. I personally do not recommend it.

 

4. format the GridView

Why format the data in the GridView?

Sometimes the data we display in the GridView is not directly stored in the database. For example, when we store a person's gender, we generally use 1 to represent men, and 0 to represent women, however, we cannot directly display 0 or 1 in the GridView. Therefore, we need to format the data read from the database, that is, the data of the bound data source.

How to format the GridView?

 

(1) Method 1: Use the DataFormatString attribute in the binding column of the GridView to format

Eg: <asp: BoundField XXXXXXX... DataFormatString = "<I >{ 0} </I>"/>

This column is displayed in italic.

 

(2) Method 2: use various operations and expressions to format the tags bound to the template column of the GridView.

Eg: <asp: TemplateField XXXXXXX...>

<ItemTemplate>

<% # Eval ("UserName"). ToString () * 1123/1234 + 100%>

</ItemTemplate>

</Asp: TemplateField>

 

(3) method 3: Call the background method in the tag bound to the template column of the GridView for formatting.

In fact, this method is very similar to method 2, but only the data formatting code is put in the background.

Eg: <asp: TemplateField XXXXXXX...>

<ItemTemplate>

<% # DealData (Eval ("UserName"). ToString () %>

</ItemTemplate>

</Asp: TemplateField>

Background code:

Int DealData (string str)

{

Return 0;

}

 

(4) Method 4: read data from the data source in RowDataBound, the binding event processing method of the GridView, and assign a value to the GridView

Eg:

GridViewRow gvr=e.Row;if(gvr.RowType == DataControlRowType.DataRow){    string sUserName = DataBinder.Eval(gvr.DataItem,"UserName").ToString();    gvr.Cells[0].Text = string.Format("<i>{0}</i>",sUserName);}

We can directly obtain the GridViewRow of the currently bound Row from e. Row. Note: each row of RowDataBound bound to the GridView will be triggered once. Then, we can determine whether this row is a data row (rather than a header or footer row). If so, we can get the data row from the DataItem of GridViewRow, after formatting, assign a value to the sUserName string, and display the string in the first column of GridViewRow. In addition, various permission judgments can be made to display different data for different users.

Related Article

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.