简体   繁体   English

WinForms安装DataGridView,其中一列绑定到List

[英]WinForms setup DataGridView with one column bound to List

I'm using C# 4.0 and WinForms. 我正在使用C#4.0和WinForms。 I'm trying to get my DataGridView to be setup like this 我正在尝试像这样设置我的DataGridView

[CheckBox]  [TextColumn]  [ComboBoxColumn]

This is for a menu similar to the SQL Server Import/Export tool. 这是用于类似于SQL Server导入/导出工具的菜单。 The checkbox will tell me whether the table is to be transferred. 复选框将告诉我是否要转移表格。 TextColumn is the source table name. TextColumn是源表名称。 And ComboBoxColumn is a list of the tables in the destination database. ComboBoxColumn是目标数据库中表的列表。

Example

[Transfer]  [SourceTable]  [DestinationTable]
    X       MyTableSource  MyTableDest

Where MyTableDest would be a list I could choose from like (TableA, TableB, TableC), or I could input my own name. MyTableDest是一个列表,我可以从中选择(TableA,TableB,TableC),也可以输入自己的名字。

I tried this.dataGridView.DataSource = myBindingList where mybindingList has my custom object that looks like 我尝试了this.dataGridView.DataSource = myBindingList,其中mybindingList具有我的自定义对象,看起来像

public class Mine {
   public bool Transfer { get; set;}
   public string Source { get; set;}
   public List<string> Destination { get; set; }
}

I don't need a full solution - just direction on how to achieve this 我不需要完整的解决方案-只是如何实现此目标的方向

The Mine class isn't holding a list of destinations, only a single destination, so your class should look like this: Mine类不包含目的地列表,而仅包含一个目的地,因此您的类应如下所示:

public class Mine {
  public bool Transfer { get; set; }
  public string Source { get; set; }
  public string Destination { get; set; }
}

Then using a DataGridView control that has those three columns you specified, this is a quick example to get it running: 然后使用具有您指定的三列的DataGridView控件,这是使其运行的简单示例:

Column1.DataPropertyName = "Transfer";
Column2.DataPropertyName = "Source";
Column3.DataPropertyName = "Destination";
Column3.DataSource = new List<string>() { "aaa", "bbb", "ccc" }; 

List<Mine> grid = new List<Mine>();
grid.Add(new Mine() { Transfer = true, Source = "xxx", Destination = "bbb" });
grid.Add(new Mine() { Transfer = false, Source = "yyy", Destination = "aaa" });
grid.Add(new Mine() { Transfer = true, Source = "zzz", Destination = "ccc" });

dataGridView1.DataSource = grid;

Column3 is the ComboBox column where you specify the list of options in the DataSource property of that column, so the list is separate from the class of Mine objects. Column3是ComboBox列,您可以在该列的DataSource属性中指定选项列表,因此该列表与Mine对象的类分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM