Retrieve column fields in the rowdatabound event of the gridview Control

Source: Internet
Author: User

I don't know what's going on today. Why can't I find the control in the gridview column? The key is to hide the control in the field during editing. The value is very problematic. I found it online, you may know this classic stuff, but it is rarely compared and sorted together. There are multiple ways to find controls. However, when the control is hard to find, you can specify its dataitem, you can use the method below 4 to quickly set the value. I personally think the most valuable information

To obtain a data column of the current row, we can summarize the following methods in practice:
1. cells [X]. txt. Obtain the text value from the column cell. This method is simple, advanced, and most commonly used, but with simple functions. This method has several disadvantages: (1) the value of the data column with hidden attributes cannot be obtained, and the obtained value is "" (null ). (2) Only data columns defined in HTML can be obtained, and all field columns of the current data row in the data source cannot be queried. In general, the number of field columns in the gridview is usually smaller than the actual number of fields in the data source through HTML settings, because from the perspective of business logic, not all fields are displayed on the page. When you need to use a field that is not displayed, this method cannot directly meet the requirements.
2. E. Row. cells [X]. findcontrol ("yourcontrolname "). This is to find a server control in a cell to obtain its data value. In this way, you can operate the server controls in cells. This method is generally used to process data or controls in the template column.
3. (datarowview) E. Row. dataitem). Row. itemarray [X]. tostring (). The core of this method is E. Row. dataitem, which is the row dataset of the gridview, which is of the object type. After converting it to the datarowview type, more operation methods can be obtained. This dataset indicates all field columns in the current row of the data source. itemarray [x] is an array object of all field columns in the current row. You can obtain any field value through index x. This method can be used to query all fields of the data source.
4. databinder. eval_r (E. Row. dataitem, "yourdatafieldname"). tostring (). This method is still based on E. Row. dataitem. It is more convenient to obtain any field value of the data source, because databinder. eval () is used, it should be noted that do not write the wrong field name.
5. Convert E. Row. dataitem to custom type to implement data type. Example: dsrequesttracking. tb_requesttrackingrow rtrow = (dsrequesttracking. tb_requesttrackingrow (datarowview) E. row. dataitem ). row; requeststatusdropdownlist1.selectedvalue = rtrow. statusid. tostring (); where dsrequesttracking uses dsrequesttracking. the XSD file is a custom strong type dataset in the project. tb_requesttrackingrow is a method that vs automatically generates for this strong type to create a data row object. This method is widely used in typed data. Data typing has many advantages. What is obvious is to pass a type of data to the method to replace a large number of parameters. In addition, the spelling of field names will no longer occur.
In summary, the above methods have their own merits and can be used properly to meet different needs in the development process.

Retrieve column fields in the rowdatabound event of the gridview Control

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.