Xamarin FlexGrid Binding Data Using DataTable not working

Posted by: famaya on 30 June 2021, 5:55 pm EST

    • Post Options:
    • Link

    Posted 30 June 2021, 5:55 pm EST

    Hello there!!!

    Im under development and Im using FlexGrid, on previous views I used an observable collection as datasource for my grid, that works fine since the data are using a fixed number of columns.

    I have this new problem, on the same view I have to display different data depending on the button the user selects… So… on 1st button I display 4 columns, on the 2nd 6 cols and so with other 5 buttons.

    To work around my problem (because of time) I declared 7 different types of data depending on the kind of data I need to be displayed. But it become so “ugly to read” on code.

    I tried instead binding a recordset, a datable, an empty list<> or a collection<> that fills up depending on the data I read from the database.

    Is there a way to display data on the FlexGrid using a System.Data.DataTable or a System.Data.SQLClient.SqlDataReader that I can “assign” directly as ItemSource and it displays the correct number of columns according to the data itself??

    This is part of my code where I create the “fixed” collection (yuck…) and use it as source for the FlexGrid

    
    SqlDataReader objRegistros = null;
                    clsRespuesta Respuesta = VariablesGenerales.objConexion.Consultar(sSQLQuery, ref objRegistros);
                    if (Respuesta.ResultadoCorrecto)
                    {
                        //SE TOMAN ALGUNOS DATOS DEL REGISTRO PARA CONFIGURAR CORRECTAMENTE EL GRID DINAMICO
                        int iColumnas = 0;
    
                        while (objRegistros.Read())
                        {
                            Columnas_PROD_MapeoInterno RegistroNuevo = new Columnas_PROD_MapeoInterno(objRegistros);
                            PROD_MapeoInterno.Add(RegistroNuevo);
                        }
                        iColumnas = objRegistros.FieldCount;
                        objRegistros.Close();
                        VariablesGenerales.objConexion.Desconectar();
                        GrdDetalle.ItemsSource = PROD_MapeoInterno;
                        GrdDetalle.AutoSizeMode = C1.iOS.Grid.GridAutoSizeMode.None;
    
    

    Im also sharing my “file” if you can use it as reference for the question…

    What I’d like to do is directly something like this…:

    
    GrdDetalle.ItemsSource = objRegistros; //where objRegistros is a SQLDataReader or can be a DataSet
    
    
  • Posted 1 July 2021, 8:11 am EST

    Hi Felix

    The file that you are talking about is not attached to this thread. Please compress your file and attach it to the thread, that will help us to assist you better.

    Thanks

  • Posted 1 July 2021, 12:27 pm EST

    Here it goes as ZipArchivo.zip

  • Posted 2 July 2021, 10:15 am EST

    Hi Felix

    We can not directly bind the datatable with the FlexGrid, however, we can use the below workaround

    
    for(int i = 0; i < table.Columns.Count; i++)
                {
                    grid.Columns.Add(new GridColumn());
                }
                for(int i=0; i <table.Rows.Count; i++)
                {
                    grid.Rows.Add(new GridRow());
                }
                for(int column = 0; column < grid.Columns.Count; column++)
                {
                    for(int row = 0; row < grid.Rows.Count; row++)
                    {
                        grid[row, column] = table.Rows[row].ItemArray[column];
                    }
                }
    
    

    Also, we can toggle the visibility of the columns of the FlexGrid on button click

    grid.Columns[0].IsVisible = false;
    

    Please let me know if you need any other help.

    Thanks

  • Posted 7 July 2021, 11:34 am EST

    Ok, will do it that way, thanks for the advice!!!

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels