locked
How to bind Object[,] to a datagridview RRS feed

  • Question

  • User-777350147 posted

    Hi,

    I have 2D array with different types of data(int, string, float).

    How do i bind it to datagridview?

    Or converting it to dataView or BindingList or DataTable and then bind it to datagridview?

    Thursday, July 7, 2016 1:13 AM

Answers

  • User-2057865890 posted

    Hi nithin.bandaru1,

    Take a look at Binding Arrays to GridView in ASP.Net, it explains how to bind arrays to grids like DataGrid GridView in asp.net

    //2-Dimensional Array
    string[,] arr2D = { 
                        { "John", "21" }, 
                        { "Smith", "33" }, 
                        { "Ryder", "15" }, 
                        { "Jake", "18"}, 
                        { "Tom","34" }
                     };
    ArrayList arrList = new ArrayList();
    for(int i=0;i<5;i++)
    {
        arrList.Add(new ListItem(arr2D[i, 0], arr2D[i, 1]));
    }
    Grid2D.DataSource = arrList;
    Grid2D.DataBind(); 

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 23, 2016 8:28 AM

All replies

  • User1724605321 posted

    Hi ,

    You should focus on rows and cells of datagridview . You need to build your DataGridView  columns and then populate each row cell by cell .Please refer to below threads for demo :

    http://stackoverflow.com/questions/29633018/show-2d-array-in-datagridview 

    http://stackoverflow.com/questions/4111308/2-dimensional-integer-array-to-datagridview 

    And since your question is related to windows form , you could post to windows form forum .

    Best Regards,

    Nan Yu

    Thursday, July 7, 2016 2:41 AM
  • User-777350147 posted

    It will be easy if the array has same type of data:

    Object[,] is completely dynamic data(Different no of rows, columns, data types., Like dump data): (Excel sheets)

    [0,x]     "kjslwe"    3        "w"        45        "erer"       643         "reew"    54    56        34

    [1,y]         23       "e"        1         "sf"       123213    "ds"         343433

    [0,z]      "ANF"     7        "X"        45        "kler"       473          "uyew"   err    76        fsd

    So i want it to convert into array list or something and then bind

    Thursday, July 7, 2016 8:53 AM
  • User-2057865890 posted

    Hi nithin.bandaru1,

    Take a look at Binding Arrays to GridView in ASP.Net, it explains how to bind arrays to grids like DataGrid GridView in asp.net

    //2-Dimensional Array
    string[,] arr2D = { 
                        { "John", "21" }, 
                        { "Smith", "33" }, 
                        { "Ryder", "15" }, 
                        { "Jake", "18"}, 
                        { "Tom","34" }
                     };
    ArrayList arrList = new ArrayList();
    for(int i=0;i<5;i++)
    {
        arrList.Add(new ListItem(arr2D[i, 0], arr2D[i, 1]));
    }
    Grid2D.DataSource = arrList;
    Grid2D.DataBind(); 

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 23, 2016 8:28 AM