簡體   English   中英

WinForms安裝DataGridView,其中一列綁定到List

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

我正在使用C#4.0和WinForms。 我正在嘗試像這樣設置我的DataGridView

[CheckBox]  [TextColumn]  [ComboBoxColumn]

這是用於類似於SQL Server導入/導出工具的菜單。 復選框將告訴我是否要轉移表格。 TextColumn是源表名稱。 ComboBoxColumn是目標數據庫中表的列表。

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

MyTableDest是一個列表,我可以從中選擇(TableA,TableB,TableC),也可以輸入自己的名字。

我嘗試了this.dataGridView.DataSource = myBindingList,其中mybindingList具有我的自定義對象,看起來像

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

我不需要完整的解決方案-只是如何實現此目標的方向

Mine類不包含目的地列表,而僅包含一個目的地,因此您的類應如下所示:

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

然后使用具有您指定的三列的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是ComboBox列,您可以在該列的DataSource屬性中指定選項列表,因此該列表與Mine對象的類分開。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM