How to bind the array type to the datagridview Control

Source: Internet
Author: User

The datasource of the dview control has the following types:

The datagridview class supports the standard Windows form data binding model. This means that the data source can be any type that implements one of the following interfaces:

    • Ilist interface, including a one-dimensional array.
    • Ilistsource interface, such as datatable and dataset class.
    • Ibindinglist interface, for example, bindinglist class.
    • Ibindinglistview interface, for example, bindingsource class.

If the array is directly bound to the datagridview control, the expected result is not displayed. The following is an exampleCode:

 Private   Void Button2_click ( Object Sender, eventargs e ){ Int [] Intarr = New   Int [2, 100]; arraylist mylist = New Arraylist ();// Generate 100 different numbers Random RND = New Random (); While (Mylist. Count <100 ){ Int Num = RND. Next (1,101 ); If (! Mylist. Contains (Num) mylist. Add (Num );} For ( Int I = 0; I <100; I ++) intarr [I] = ( Int ) Mylist [I]; This . Datagridview1.datasource = intarr ;}

When you run this code, no error is reported, but the datagridview does not display the 100 different numbers in the intarr array, but nothing.

 

There are three common methods to display intarr data to the datagridview.

1. Put the data into a datatable.

 Private   Void Button2_click (Object Sender, eventargs e ){ Int [] Intarr = New   Int [2, 100]; arraylist mylist = New Arraylist (); // Generate 100 different numbers Random RND = New Random (); While (Mylist. Count <100 ){ Int Num = RND. Next (1,101 ); If (! Mylist. Contains (Num) mylist. Add (Num );} For (Int I = 0; I <100; I ++) intarr [I] = ( Int ) Mylist [I]; datatable dt = New Datatable (); datacolumn Dc = New Datacolumn ( "Number" ); DT. Columns. Add (DC ); Foreach ( Int S In Intarr) {DT. Rows. Add (s );} This . Datagridview1.datasource = DT ;}

The running result is as follows:

 

Because datasource binds object attributes, you can use the container class to create an object array.

         Protected   Class Item {Private   Int Test_value; Public Item ( Int Value ){ This . Test_value = value ;} Public   Int Value {get { Return Test_value;} set {test_value = Value ;}}} Private   Void Button2_click ( Object Sender, eventargs e ){Int [] Intarr = New   Int [2, 100]; arraylist mylist = New Arraylist (); // Generate 100 different numbers Random RND = New Random (); While (Mylist. Count <100 ){ Int Num = RND. Next (1,101 ); If (! Mylist. Contains (Num) mylist. Add (Num );} For ( Int I = 0; I <100; I ++) intarr [I] = ( Int ) Mylist [I]; item [] itemarr = New Item [100]; For ( Int I = 0; I <100; I ++) itemarr [I] = New Item (intarr [I]); This . Datagridview1.datasource = itemarr ;}

The above item class is a container class. Code running result

 

This method is the simplest. Directly convert each element in the array into an array class for display.

  Private   Void Button2_click ( Object Sender, eventargs e ){ Int [] Intarr = New   Int [2, 100]; arraylist mylist = New Arraylist (); // Generate 100 different numbers Random RND = New Random (); While (Mylist. Count <100 ){ Int Num = RND. Next (1,101 ); If (! Mylist. Contains (Num) mylist. Add (Num );} For ( Int I = 0; I <100; I ++) intarr [I] = ( Int ) Mylist [I]; This . Datagridview1.datasource = (from number In Intarr select New {Number}). toarray ();}

This is the running result graph, and number is the column name.

 

Of course, there must be other methods to implement it. Here we just give three examples as a common method.

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.