Detailed description of gridview

Source: Internet
Author: User
Preface In ASP. net2.0, The gridview is probably the most commonly used data list control (repeater)
The following describes the usage of the gridview from setting its data source to its data binding, and then to its display settings. (it can be seen that this article is just a summary post, and there is no technical content. Please note that)

 

No. 1 Data source:
For the gridview, as long as the serialized data can be used as a data source, such as an object array/XML/table, as long as you can imagine, it can be converted into a table.
If the chart is easy to use, you can use it with the datasource control, and then the operations on the data can be concentrated in the datasource.
To bind a data source, follow these steps: 1. Specify the Data source: gridview. datasource = xxx; 2. Bind The Data source: gridview. databind ();

No. 2 Data Binding:
The gridview automatically generates columns Based on Data by default after we bind the data source. However, sometimes, we manually specify the data source instead of the datasource control, and more diverse data binding and control are required. Therefore, using the template column to bind data and positioning the control in the template becomes the key: (here I omit the data bound with the built-in columns in the gridview. If there is time, I will put this in the bottom appendix of this article)

1. Set template Columns
In design mode, a small triangle exists in the upper-right corner of the gridview. Click it to perform more operations on the gridview, such as column editing. If a template column is added, you can also edit the template column here.

In "display", click the drop-down list and select "item template" (this is the main display area, and the displayed items will be displayed in the normal state of the gridview ), we can enter anything in it (basically all controls that can display data can be put in to help us bind and display data ).
For example, we drag a label and click the triangle in the upper-right corner of the label to set its data binding.
If you use a set datasource control as the data source, you can directly select the field to be bound and Its display format. The system will automatically generateCode;
If you specify the data source yourself ..... then we have to set the binding by ourselves. Here we can directly enter eval ("fields to be displayed"), switch to the Code, and write in it. for label, the possible syntax is: <asp: Label id = "label1" runat = "server" text = '<% # eval ("ID ") %> '> </ASP: Label>.
The biggest advantage of self-writing <% # eval ("ID") %> is that we can enter all the code we need in it! For example: <% # convert. tostring (convert. toint32 (eval ("ID") + 100) %>
Look, this means we can modify the bound data and display it again. if the data control is similar to a gridview that can help set data columns, we can even specify a function to return the data source!

In "display", click the drop-down list and select edititem template (the displayed items will be displayed in the editing state of the gridview-that is, the gridview. editindex is equal to the row), we need to put into it what we want to display when editing the data, such as a Textbox (here, you can also specify the data to be bound to the control ).
The code may be like this: <asp: textbox id = "textbox1" runat = "server" text = '<% # eval ("ID") %>'> </ASP: textbox>
Then all the code in the gridview may be like this:
<Columns>
<Asp: templatefield>
<Edititemtemplate>
<Asp: textbox id = "textbox1" runat = "server" text = '<% # eval ("ID") %>'> </ASP: textbox>
</Edititemtemplate>
<Itemtemplate>
<Asp: Label id = "label1" runat = "server" text = '<% # eval ("ID") %>'> </ASP: Label>
</Itemtemplate>
</ASP: templatefield>
<Asp: commandfield showeditbutton = "true"/>
</Columns>
Add a rowediting event to the gridview:
Protected void gridviewinclurowediting (Object sender, gridviewediteventargs E)
{
This. gridview1.editindex = E. neweditindex;
}
Run the command and click Edit. The displayed data is put into a texbox, so that we can edit it.

2. in the rowdatabound event of the gridview, set detailed Binding data
the template column above controls the detailed display of most data after data binding. however, some people may think that this writing is not very nice (the page and Code are not separated at all ), or the binding error caused by using usercontrol in the gridview (because usercontrol has not been initialized when the gridview binds data to the control, therefore, you can perform detailed binding settings in this event.
the event is triggered only after a row of the gridview is bound. because the Specific Row binding involves the location of intra-row data, we will put it below to illustrate how to use rowdatabound. Here, we will talk about its process: first verifying the returned e. whether the row is completely created, and then from E. use findcontrol (controlid) in row to obtain the control whose ID is controlid, and assign a value to the control. it should be noted that if the built-in columns of the gridview are used, different methods are used in rowdatabound to find the columns.
since data binding is fully implemented in the background code, we can modify and bind data more freely.

Control/Data positioning in No. 3 griview:
Searching for a data in the gridview is not simply a this. gridview1.xxx is so simple, because when we write code in the background, the data/data template in the gridview has not yet been generated. so in general, the method we use is to first determine the row where the data/data template is located, and then extract it from the row according to its type (here we can see that, this is also an assumption that the row has been completely created and contains the data/data template we are looking for-So before using it, make sure that before executing the code, the line required by the code has been created ).
You can use this. gridview1.rows [Index]. cells [Index]. text;
For the template column, use this. gridview1.rows [Index]. cells [Index]. findcontrol (string controlid) or this. gridview1.rows [Index]. findcontrol (string controlid ).
Check whether the row of the gridview is created. You can use row. rowtype = datacontrolrowtype. datarow (where row is the row of the gridview to be checked ).

The following describes two common positioning methods, each with its own advantages and disadvantages. When using events, a row is often obtained. When using the button control to bind data, you can obtain specific data according to the bound data.
1. Use the built-in events of the gridview for locating:
Below I will list the events that may be used (here the default Control name is gridview1)
Protected void gridviewincluselectedindexchanging (Object sender, gridviewselecteventargs E)
{
// This method can be used when the Select column of the gridview is used
// E. newselectedindex; // the row to be selected. The index of the current page of The gridview is obtained.
}
Protected void gridview1_selectedindexchanged (Object sender, eventargs E)
{
// This. gridview1.selectedvalue. tostring ();
// The datakeys of the row to be selected. The datakeys of the current page of The gridview are obtained,
// For example, you can set the datakeys of the gridview to the "ID" of the data source. The data ID of the selected row is obtained here.
// Note that selectedvalue has a value only in selectedindexchanged and is null in other cases;
}
Protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs E)
{
// E. rowindex; // the row to be submitted. The index of the current page of The gridview is obtained.
}
Protected void gridviewinclurowediting (Object sender, gridviewediteventargs E)
{
// E. neweditindex; // the row to be edited. The index of the current page of The gridview is obtained.
}
Protected void gridview1_rowdeleting (Object sender, gridviewdeleteeventargs E)
{
// E. rowindex; // the row to be deleted. The index of the current page of The gridview is obtained.
}
Protected void gridview1_rowcancelingedit (Object sender, gridviewcancelediteventargs E)
{
// E. rowindex; // the row to unedit. The index of the current page of The gridview is obtained.
}
Protected void gridview1_pageindexchanging (Object sender, gridviewpageeventargs E)
{
// E. newpageindex; // the page to be retrieved. The index of the page to be switched by the gridview is obtained.
}
Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow & E. Row. rowindex! = Gridview1.editindex)
{
// E. Row; // The row that has been generated and bound to the data;
}
}
2. Use the commandargument attribute in the button to bind the ID to obtain the row to be edited, deleted, submitted, and selected.
For example, the possible code is as follows: <asp: button id = "button1" runat = "server" commandargument = '<% # eval ("ID") %>'
Onclick = "button#click" text = "button"/>
Then in the button#click event, we can use (button) sender). commandargument to obtain the bound data.
3. Use gridview. XXX for locating.
The problem is back. Now we want to discuss how to use the common and familiar method of gridview to obtain the data we want from the gridview.
In the gridview, two types of data can be stored: one that can be displayed and presented to us in the form of a table, that is, we usually use the gridview. row or column to obtain; one is not displayed, but we can use it in the background, you can use the gridview. datakeys to set and obtain its value;
No matter what type of data, as long as it is not the property of the gridview, We need to completely generate the gridview before obtaining.
1) Data Table
As mentioned above, the gridview only generates a table for the data, so the method to obtain the data is nothing more than the gridview. row (gridview. column is mainly used to set the attributes of the entire column, so it is generally not used to get the values in it). The following is an example. I add a small attribute to the "btnadd" button in the 2nd Column of each row:
Foreach (gridviewrow GR in gridview)
{
(Button) gr. cells [1]. findcontrol ("button"). Attributes. Add ("onclick", "alert ('clicked me! ')");
}
2) datakeys
If it is data in datakeys, you can directly use the gridview. datakeys [number or the name of the bound key] to obtain. for example, I set the gridview at the beginning. datakeynames = "ID"; then, after all the data is bound, you can use the gridview. datakeys ["ID"] to obtain the value of the data ID of this row.
The advantage is obvious. If I want to obtain the specific field value of the row without displaying it on the page, this method is better.
Let's talk about it first. I will add more to the rest. You are welcome to discuss and give some comments.

 

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.