简体   繁体   中英

WinForms setup DataGridView with one column bound to List

I'm using C# 4.0 and WinForms. I'm trying to get my DataGridView to be setup like this

[CheckBox]  [TextColumn]  [ComboBoxColumn]

This is for a menu similar to the SQL Server Import/Export tool. The checkbox will tell me whether the table is to be transferred. TextColumn is the source table name. And ComboBoxColumn is a list of the tables in the destination database.

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.

I tried this.dataGridView.DataSource = myBindingList where mybindingList has my custom object that looks like

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:

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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