WPF Data Grid – Bind to a gRPC Service

WPF Team Blog
24 June 2021
As you probably know, the DevExpress WPF Data Grid can be bound to a gRPC service. gRPC is a modern, high-performance remote procedure call (RPC) framework that can run on any platform. Our Virtual Sources allow you to connect to a gRPC service easily.
In this post, we'll describe how to connect to a gRPC service and use our Virtual Sources to display gRPC data within the WPF Data Grid.
To connect to a gRPC service, you must first create a client object to access service methods:
  1. Specify the channel that connects to a gRPC service.
  2. Pass this channel as an argument to a new instance of the IssuesService.IssuesServiceClient class:
Channel channel;
IssuesService.IssuesServiceClient client;
public IssueServiceViewModel() {
    channel = new Channel("127.0.0.1:30051", ChannelCredentials.Insecure);
    client = new IssuesService.IssuesServiceClient(channel);
    // ...
}

[Command]
public void Closed() {
    channel.ShutdownAsync().Wait();
}
When you build the application, it will automatically generate IssuesServiceBase and IssuesServiceClient classes based on the .proto file. You can view the complete .proto file with request and response methods in \IssuesData\issuesdata.proto.
To proceed, create a command that calls the service's FetchIssuesAsync method. In this command, specify a request protocol buffer object (Fetch in this example). Pass this object to the client's FetchIssuesAsync method. When the remote procedure call (RPC) is complete, the service returns a response protocol buffer object (Issues in this example).
[Command]
public void FetchIssues(FetchRowsAsyncArgs args) {
    args.Result = GetIssuesAsync(args);
}
async Task<FetchRowsResult> GetIssuesAsync(FetchRowsAsyncArgs args) {
    var take = args.Take ?? 30;
    var issues = await client.FetchIssuesAsync(new Fetch {
        Skip = args.Skip,
        Take = take,
        Sort = GetIssueSortOrder(args.SortOrder),
        Filter = (IssueFilter)args.Filter
    });
    var issuesArray = issues.Items.ToArray();
    return new FetchRowsResult(issuesArray, hasMoreRows: issuesArray.Length == take);
}
Assign the InfiniteAsyncSource virtual source to the GridControl's ItemsSource property and bind the FetchIssues command to the virtual source's FetchRowsCommand property:
<dxg:GridControl.ItemsSource>
    <dx:InfiniteAsyncSource ElementType="{x:Type issuesdata:Issue}" 
                            FetchRowsCommand="{Binding FetchIssuesCommand}"/>
</dxg:GridControl.ItemsSource>

REST Services, Custom WCF Services and More

Virtual Sources also allow you to connect the DevExpress WPF Data Grid to other services including REST Services, Custom WCF Services, etc.
If you'd like to explore this topic further, please review the following at your convenience: Virtual Sources Overview.

Your Feedback

As always, should you have any feedback or questions, please submit your comments below or create a ticket on the DevExpress Support Center.

FREE DevExpress Xamarin UI Controls

Deliver high-impact native mobile apps with our feature-rich Data Grid, Scheduler, Chart, TabView, Editors, and utility controls. Reserve your free copy today!
Jaime Alvarez [VOLUNDAT]
Jaime Alvarez

When will be available for Blazor Data Grid – Bind to a gRPC Service?


Regards

24 June 2021
Konstantin Balashov
Konstantin Balashov
When will be available for Winforms Data Grid – Bind to a gRPC Service?
24 June 2021
Alex Chuev (DevExpress)
Alex Chuev (DevExpress)

@Jaime and Konstantin:


In WinForms, you can use the VirtualServerModeSource class to connect a Data Grid to a gRPC service, as long as your Data Grid does not require grouping:

https://supportcenter.devexpress.com/ticket/details/t993983/how-to-bind-winforms-data-grid-using-virtual-sources-and-grpc-service


In Blazor, the same result can be achieved using the CustomData property:

https://supportcenter.devexpress.com/ticket/details/t942785/datagrid-for-blazor-how-to-use-the-grpc-service-with-the-customdata-property


Thanks,
Alex

2 July 2021
Vladimir Frizen (DevExpress)
Vladimir Frizen (DevExpress)

Hi Jaime,


We do not plan to support binding to a gRPC service in our Blazor components in the near time.

2 July 2021

Please login or register to post comments.