TX Sheet Control

IDataSource.QueryAsync Method

Method
Namespace
TXTextControl.TXSheetControl.Data
Package
TXTextControl.TXSheetControl.Server as TXTextControl.TXSheetControl.Data.dll from the private NuGet feed or the supplied NuGet folder

Returns no more than request.Limit rows. Cancellation must cover opening, reading and disposal.

Syntax

public Task<DataSnapshot> QueryAsync(DataRequest request, CancellationToken cancellationToken = default)

Parameters

ParameterTypeOptionalDescription
requestDataRequestNoValue matching request's declared type and the owning object's validation rules.
cancellationTokenCancellationTokenYesCancels waiting or execution as supported by this operation.

Return Value

Task<DataSnapshot>.

Examples

Request a bounded snapshot from an application-provided data source, honor its paging capability and write the returned columns and rows into a report.

using TXTextControl.TXSheetControl.Data;
using TXTextControl.TXSheetControl.Server;

async Task<byte[]> ExportProviderDataAsync(ServerSheetControl control, IDataSource source)
{
    var book = control.Workbook;
    var sheet = await book.Sheets.AtAsync(0);
    var request = new DataRequest { Limit = 100 };

    if (!source.Capabilities.HasFlag(DataSourceCapabilities.Paging)) request = new DataRequest();

    var data = await source.QueryAsync(request);

    if (data.Rows.Count == 0) return await control.SaveAsync(StreamType.PDF);

    var lastColumn = "";

    for (var index = data.Columns.Count; index > 0; index /= 26) { index--; lastColumn = (char)(65 + index % 26) + lastColumn; }

    await sheet.Range("A1:" + lastColumn + "1").SetValuesAsync([data.Columns.Cast<object?>().ToArray()]);
    await sheet.Range("A2:" + lastColumn + (data.Rows.Count + 1)).SetValuesAsync(data.Rows.Select(row => row.ToArray()).ToArray());
    return await control.SaveAsync(StreamType.PDF);
}

Remarks

Use the member with an instance or settings object from the documented package. Await task-returning operations. Document-bound handles must be resolved again after loading or deleting their owner.

See Also