ASP. NET-gridview Data Binding Methods

Source: Internet
Author: User

 

Prerequisites: fields in multiple tables must be displayed on the page.

Method 1: Perform cascade queries directly and set the result setFill the dataset with sqldataadapter, and then bind the dataview of the able to the data source of the gridview ,:

// Connect to the database and obtain the returned results;

Public dataset getresult (string str1)

{

String STR = "database connection string ";

String str1 = "cascade query statement"

Sqlconneciton sconnection = new sqlconnection (STR );

Sqlcommand scommand = new scommand ();

Scommand. Connection = sconnection;

Scommand. Textcommand = str1;

Sqldataadapter adapter = new sqldataadapter ();

Adapter. Selectcommand = scommand;

Dataset DS = new dataset ();

Adapter. Fill (DS );

Return Ds;

}

Page layer. CS code fragment:

Protected void page_load ()

{

This. gridview1.datasource = Ds. Tables [0]. defaultview;

This. gridview1.databind ();

}

Page layer. Aspx file code fragment1: binds all fields in dataview to the page in the order of dataview.

<Asp: gridview id = "girdview1"> </ASP: gridview>

----------------------------------------------------------------

Page Layer 1. Aspx file code fragment2: bind only some fields and change the order rules of dataview. For example, in dataview, the name column is displayed in the third place, And the ID is in the second place; on the page, you must not display the ID column, but only the name column, which is arranged in the second place:

<Asp: gridview id = "gridview1">

<Asp: templatefield>

<Itemtemplate>

<% # (Datarowview) container. Dataitem) ["name"]. Tostring () %>

</Itemtemplate>

</ASP: templatefield>

<Asp: templatefield>

<Itemtemplate>

<% # Databinder. eval (container. Dataitem, "name") %>

</Itemtemplate>

</ASP: templatefield>

<Asp: templatefield>

<Itemtemplate>

<% # Databinder. eval (container, "dataitem. Name ") %>

</Itemtemplate>

</ASP: templatefield>

</ASP: gridview>

Bytes ------------------------------------------------------------------------------------------------------------------------------

Method 2: retrieve the records to be displayed in a table, assign values to N info objects one by one, and save all info objects in a collection class (for example: list <>, Dictionary, collection, arraylist, and hashtable. Note that these classes are non-thread-safe.) then, bind the collection class to girdview.

However, a girdview can only be bound to one collection class. Because fields in other tables are missing, we must query the name in Table B through the ID in table. Therefore, we need to add the query name to the CS file, BLL layer, and Dal layer on the page layer:

Because of ASP. Net can be bound to methods, properties, control properties, and so on, so this method works:

<Asp: gridview id = "gridview1">

<Asp: templatefield>

<Itemtemplate>

<% # Getname (xxxinfo) container. Dataitem ). Id. Tostring () %>

</Itemtemplate>

</ASP: templatefield>

<Asp: templatefield>

<Itemtemplate>

<% # Getname (databinder. eval (container. Dataitem, "name") %>

</Itemtemplate>

</ASP: templatefield>

<Asp: templatefield>

<Itemtemplate>

<% # Getname (databinder. eval (container, "dataitem. Name ") %>

</Itemtemplate>

</ASP: templatefield>

<Asp: templatefield>

<Itemtemplate>

<% # Getname (databinder. eval ("name") %>

</Itemtemplate>

</ASP: templatefield>

</ASP: gridview>

// Note: The getname method is written on the page. In the CS file, the method is empty. Return BLL's getname method, and then return Dal's getname method.

Bytes ------------------------------------------------------------------------------------------------------------------------------

Conclusion: The second method has poor performance because I need to access the database twice and there are many more methods than method 1.

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.