将多个列表绑定到DataGridView [英] Bind Multiple Lists to a DataGridView

查看:191
本文介绍了将多个列表绑定到DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个静态列表要绑定到一个 DataGridView 以及两个按钮字段。我只能绑定一个列表和一个按钮字段。可以将多个列表绑定到 DataGridView

I have two static lists which are to be bound to a DataGridView along with two button fields. I could only bind one list and a button field. Is it possible to bind more than one list to the DataGridView?

ServiceController objSAVAdminService = new ServiceController("SAVAdminService");
ServiceController objSAVService = new ServiceController("SAVService");
ServiceController objPPVService = new ServiceController("PPVService");
ServiceController objMLLService = new ServiceController("MLLService");

List<string> ServiceName = new List<string>();
List<string> ServiceStatus = new List<string>();

ServiceName.Add(objSAVService.ServiceName.ToString());
ServiceName.Add(objSAVAdminService.ServiceName.ToString());
ServiceName.Add(objPPVService.ServiceName.ToString());
ServiceName.Add(objMLLService.ServiceName.ToString());

ServiceStatus.Add( objSAVService.Status.ToString());
ServiceStatus.Add( objSAVAdminService.Status.ToString());
ServiceStatus.Add( objPPVService.Status.ToString());
ServiceStatus.Add( objMLLService.Status.ToString());

我需要将这两个列表绑定到一个 DataGridView 并且每行上的两个按钮可以启动或停止相应的服务。

I need to bind these two lists to a DataGridView and also two buttons on each row to start or stop the corresponding service.

谢谢。

推荐答案

开箱即用支持数据绑定 DataGridView 和两个列表。我可以看到两个主要选项:

Out of the box there is no support of a data bound DataGridView and two lists. I can see two main options available to you:


  1. 放弃数据绑定 -
    您不需要将列表设置为 DataGridView DataSource 属性,并依靠DataBinding来使所有工作正常。相反,您可以使用未绑定模式,并编写代码以自动移动数据。看看 MSDN 上的这个简单示例。

  1. Abandon DataBinding -- You don't need to set a list to the DataGridView DataSource property and rely upon DataBinding to get everything working. Instead you can work in unbound mode and write code to move data back and forwards yourself. Have a look at this simple example on MSDN.

创建自定义对象 - 如果您想要方便数据绑定,您可以创建一些组合两个列表,然后绑定到那个。如果您只想要单向数据绑定(当 DataGridView 更改时,不更新列表中的数据),那么您甚至可以使用一个简单的LINQ查询来执行此操作。否则,您可以编写一些自定义对象,作为两个列表中的包装器 - 如果您设计好,您应该能够在两个列表中拥有完整的双向数据绑定。

Create a Custom Object -- If you want the convenience of data binding you can create some object that combines the two lists, and then bind to that. If you only want one way data binding (not updating the data in the lists when the DataGridView changes) then you can even do this with one simple LINQ query. Otherwise you can write some sort of custom object that acts as a wrapper over the two lists - if you design it well you should be able to have full two way data binding over your two lists.

这篇关于将多个列表绑定到DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆