Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when binding 2D Array to Gridview

I want to bind a multidimensional array to gridview, but it display the following error when running "Gridview1.DataBind()"

"Array was not a one-dimensional array."

The following is my code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        CreateArray()
End Sub

Private Sub CreateArray()

        Dim Array(4, 3) As String
            Array(0, 0) = "ABC"
            Array(1, 0) = "DEF"
            Array(2, 2) = "GHJ"
            Array(1, 3) = "XVZ"

        Gridview1.DataSource = Array       
        Gridview1.DataBind() 

End Sub

Is there any mistake in my code ? Thanks,

Joe

like image 581
Joe Yan Avatar asked Feb 12 '26 05:02

Joe Yan


2 Answers

You can't bind a 2D Array to a Gridview. It should be either a Single Dimensional array, a DataTable, or a Collection to bind data to a GridView.

like image 101
Muhammad Akhtar Avatar answered Feb 14 '26 19:02

Muhammad Akhtar


Use a DataTable instead of the MultiDimmensional array. These will bind nicely to a GridView (or other data controls).

http://www.issociate.de/board/goto/880762/Binding_Multidimensional_Array_to_DataGrid.html

like image 43
Saurabh Avatar answered Feb 14 '26 21:02

Saurabh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!