Binding a scalar array to a data Web Control

Source: Internet
Author: User

Ref: http://aspnet.4guysfromrolla.com/demos/printPage.aspx? Path =/articles/082504-1.aspx

Introduction
The ASP. NET data Web controls-The DataGrid, datalist, and repeater-are highly versatile controls that can display arbitrary collections of data. Specifically, these controls can display any data that implements eitherIenumerableInterface orIlistsourceInterface, which include things like arrays, collections (like arraylists, HashTables, queues, custom stronugly-typed collection classes, and so forth), datareaders, datatables, and datasets. these objects are bound to a data web control through two lines of code:

    1. The object is assigned to the Data Web Control'sDatasourceProperty,
    2. The Data Web Control'sDatabind ()Method is called.

at this point, the Data Web Control enumerates the datasource one record at a time. typically each record has a set of fields. for a database query, each record is a row returned from the database, and each field is a column returned by the SQL query. for a custom, stronugly-typed collection of business objects, each record is a business object instance and the fields are the public properties of the business object. (see Displaying Custom classes in a DataGrid for more information on displaying objecm objects in a data web control .)

the syntax for displaying a participant field in a data web control depends upon the data web control being used. for a DataGrid, you can use a boundcolumn, setting its datafield property to the name of the field. for a datalist or repeater (or a DataGrid's templatecolumn), you use the databinding syntax like so: <%# databinder. eval (container. dataitem, " fieldname ") %> . But how do you display data bound that the data web control that has no fields? For example, a scalar array-such as an array of integers-does not contain any fields, per se, but can definitely be bound to a data web control, since Arrays can be bound to Data Web controls. but what's the syntax to display the array values? In this article we'll examine how to accomplish this!

Displaying a scalar array with a DataGrid
As with rich objects like datasets or datareaders, the steps for displaying an array of scalar values differs whether you're using a DataGrid or if you're using a datalist or repeater. let's first look at displaying a scalar array in a DataGrid. with the DataGrid there are two options:

    1. Let the DataGrid automatically generate the columns.
    2. Generate the columns explicitly, using a single templatecolumn.

For option 2 The syntax is precisely the same as the syntax used for the datalist or repeater controls, so we'll save that discussion for later. for the first option, the simple solution is to leaveAutogeneratecolumnsProperty to its default value-true. (if you use the property builder Through Visual Studio. net, simply leave the "Create columns automatically at runtime" checkbox checked .) this will create a DataGrid with a single row whose header has the text "item ". (You Might Want To SetShowheaderTo false .)

The following code snippet shows an example ASP. NET web page that binds an integer array to a DataGrid with itsAutogeneratecolumnsProperty set to true, followed by the output.

<SCRIPT runat = "server"> private sub page_load (byval sender as system. object, byval e as system. eventargs) dim I as integer dim fib (10) as integer fib (0) = 1 fib (1) = 1 for I = 2 to 10 fib (I) = fib (I-1) + fib (I-2) Next me. dgarrayautogenerate. datasource = fib me. dgarrayautogenerate. databind () end sub </SCRIPT> <asp: DataGrid id = "dgarrayautogenerate" runat = "server"Autogeneratecolumns = "true"> </ASP: DataGrid>

[View a live demo!]

As you can see in the live demo, The DataGrid displays a single column with the values of the integer array.

Displaying a scalar array with template
While using the DataGrid'sAutogeneratecolumnsFunctionality is nice, there are times when you might want to display a scalar array in a repeater or datalist, as to have more control over the HTML markup that surrounds the scalar array's values. displaying the value of each scalar array item in a datalist or repeater-or in a templatecolumn in the DataGrid-is fairly simple, just use the following databinding Syntax:<# Container. dataitem %>. That's it! The following example extends strates displaying an integer array in a datalist.

<SCRIPT runat = "server" Language = "C #"> private void page_load (Object sender, system. eventargs e) {int [] fib = new int [11]; FIB [0] = 1; FIB [1] = 1; for (INT I = 2; I <11; I ++) fib [I] = fib [I-1] + fib [I-2]; dlfibs. datasource = fib; dlfibs. databind () ;}</SCRIPT> <asp: datalist id = "dlfibs" runat = "server" repeatcolumns = "3" gridlines = "both" cellpadding = "10" repeatdirection = "horizontal"> <itemtemplate> <B><% # Container. dataitem %></B> </itemtemplate> </ASP: datalist>

[View a live demo!]

As you can see in the live demo, the integer array is displayed in a three-columned datalist control, each value displayed inBold.

Conclusion
While typically the data you'll all display in a data web control will have fields, there may be times where you want to display a scalar array. as we saw in this article, it's quite possible to accomplish this with not only the DataGrid, but also with the datalist and repeater.

Happy programming!

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.