c# - How do I show the contents of this array using DataGridView? -
i created 2 dimensional array of strings , populated it. try bind datagrid control so:
string[][] array = new string[100][]; datagridview.datasource = array; instead of seeing contents of array see following columns: length, longlenth, rank, syncroot, isreadonly, isfixedsize, issyncrhonized.
so instead of displaying contents of array, displays properties of array. did wrong?
when allow grid control auto-generate columns, enumerate through properties of object , create column each one. has no way know want display grid of array values.
you'll need create new object (such enumerable list of class) out of array properties want bind columns. quick way use anonymous type, built using linq query. like:
string[][] array = new string[100][]; for(int = 0; < 100; i++) // set values test array[i] = new string[2] { "value 1", "value 2" }; datagridview.datasource = (from arr in array select new { col1 = arr[0], col2 = arr[1] }); page.databind(); here, we're iterating through 100 elements of array. each element array of 2 strings. we're creating anonymous type out of 2 strings. type has 2 properties: col1 , col2. col1 set array index 0, , col2 set array index 1. then, we're building grid enumeration of anonymous types. like:

you can of course define how columns created setting autogeneratecolumns false, , populated columns collection. can done declaratively within aspx file.
Comments
Post a Comment