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.