C# C1FlexGrid Virtualization with DataTable

Posted by: sergio.rodrigues on 6 March 2023, 8:43 pm EST

  • Posted 6 March 2023, 8:43 pm EST - Updated 7 March 2023, 2:19 am EST

    Hi. Is it possible to implement this sample of virtualization:

    public class VirtualModeCollectionView : C1VirtualDataCollection<Customer>
    {
       public int TotalCount { get; set; } = 1_000;
       protected override async Task<Tuple<int, IReadOnlyList<Customer>>> GetPageAsync(int pageIndex, int startingIndex, int count, IReadOnlyList<SortDescription> sortDescriptions = null, FilterExpression filterExpression = null, CancellationToken cancellationToken = default(CancellationToken))
       {
          await Task.Delay(500, cancellationToken);//Simulates network traffic.
          return new Tuple<int, IReadOnlyList<Customer>>(TotalCount, Enumerable.Range(startingIndex, count).Select(i => new Customer(i)).ToList());
       }
    } 

    but instead of a customer class, using a DataTable or DataRow implementation?

    A possible implementation would be:

    public class VirtualModeCollectionView : C1VirtualDataCollection<DataRow>
    {
        private readonly string _connectionString;
        private readonly string _selectStatement;
        private readonly int _totalCount;
    
        public VirtualModeCollectionView(string connectionString, string selectStatement, int totalCount)
        {
            _connectionString = connectionString;
            _selectStatement = selectStatement;
            _totalCount = totalCount;
        }
    
        protected override async Task<Tuple<int, IReadOnlyList<DataRow>>> GetPageAsync(int pageIndex, int startingIndex, int count, IReadOnlyList<SortDescription> sortDescriptions = null, FilterExpression filterExpression = null, CancellationToken cancellationToken = default)
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                await connection.OpenAsync(cancellationToken);
    
                var command = new SqlCommand(_selectStatement, connection);
                command.CommandType = CommandType.Text;
                command.Parameters.AddWithValue("@StartingIndex", startingIndex);
                command.Parameters.AddWithValue("@Count", count);
                using (var adapter = new SqlDataAdapter(command))
                {
                    var dataTable = new DataTable();
                    adapter.Fill(startingIndex, count, dataTable);
                    return new Tuple<int, IReadOnlyList<DataRow>>(_totalCount, dataTable.Rows.Cast<DataRow>().ToList());
                }
            }
        }
    }
    

    The problem I’m having is, when the information is being shown on grid it shows this:

    These Coumns are properties related to DataRows, and not DataTable columns.

    How can I change this code to reflect Datatable columns?

  • Posted 9 March 2023, 12:49 am EST

    Hi Sergio,

    Apologies for the delay in response. It seems that you have created a similar case on our support portal as well (Case ID: CAS-38591-K9V8L7). We have replied to your query there, and we request you to please continue the thread there only to avoid confusion.

    Best Regards,

    Kartik

  • Posted 11 January 2024, 9:27 pm EST - Updated 11 January 2024, 9:56 pm EST

    Hey Sergio & Kartik, I’m running into the same issue while using DataTable and DataRow in the data virtualization doc code. Did you find any solution for that? And can you group data with aggrigate with this?

    public class VirtualModeCollectionView : C1VirtualDataCollection<DataRow>
    {
        private DataTable _data; // Store the DataTable
    
        public VirtualModeCollectionView(DataTable data)
        {
            _data = data;
            TotalCount = _data.Rows.Count; // Set total count from DataTable
        }
    
        public int TotalCount { get; set; } = 0;
    
        protected override async Task<Tuple<int, IReadOnlyList<DataRow>>> GetPageAsync(int pageIndex, int startingIndex, int count, IReadOnlyList<SortDescription> sortDescriptions = null, FilterExpression filterExpression = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            DataRow[] rows = _data.Rows.Cast<DataRow>().ToArray();
            
            int endIndex = Math.Min(startingIndex + count, rows.Length);
            return new Tuple<int, IReadOnlyList<DataRow>>(TotalCount, rows.Skip(startingIndex).Take(endIndex - startingIndex).ToList());
        }
    }
  • Posted 11 January 2024, 10:58 pm EST

    Hi Samuel,

    Thank you for sharing your use case. We are getting in touch with the development team to get an update on this and will let you know as soon as possible.

    Thank you for your patience.

    Best Regards,

    Kartik

  • Posted 15 January 2024, 2:55 pm EST

    Hi Kartik,

    Thank you for the prompt response and for reaching out to the development team. I appreciate your efforts in looking into my use case.

    Looking forward to hearing the update and thank you for your continued assistance.

  • Posted 16 January 2024, 8:00 pm EST

    Hi Samuel,

    The development team is currently working on this behavior and has provided an ETA for the 2023v3 HF2 release.

    Thank you for your patience and understanding.

    Best Regards,

    Kartik

Need extra support?

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

Learn More

Forum Channels