2014-12-03 13 views
0

我有一個datagrid視圖,我需要從數據庫中填充內容。 我的數據庫內容是在一個數據表,通常是這樣的:Datagridview數據數據庫中的一個單元格是一個組合框的數據人口#

PROD ID PRODNAME版本

1 ABC 1.1

1 ABC 2.1

1 ABC 3.1

2高清3.1

2 def 3.2

3 GHI 1.1

4 JKL 1.1

4 JKL 1.2

現在我的問題是,當我顯示在數據網格視圖中的內容,我希望它被顯示爲這樣,其中版本應該是一個下拉comboboxcell所以每個產品都有版本的列表:

PROD ID PRODNAME版本

1 ABC 1.1

2.1 

    3.1 

2個高清3.1

3.2 

3 GHI 1.1

4 JKL 1.1

1.2 

請幫我實現這個。我不能直接說:
dataGridView1.DataSource = getdatatable()

所以請不要暗示,因爲它給了我一個沒有組合框的平面視圖。熱切期待積極的答覆。是否可以在datagrid視圖中繪製每行,並在該行中填充產品所有可用版本的組合框?請幫忙。 TIA

回答

3

本質上,您需要對您查詢的數據進行排序,使用所有版本的列表保存每個獨特的產品。然後,您將手動創建DataGridView的列,如下所述。

模擬出這種情況下,我創建了以下對象類:

// The type of binding object for your dgview source. 
public class Product 
{ 
    public Product() 
    { 
    this.Version = new List<double>(); 
    } 

    public int ID { get; set; } 
    public string Name { get; set; } 
    public List<double> Version { get; set; } 
} 

在您的查詢返回的所有對象,我會做這樣的事情:

public BindingList<Product> YourQueryCall() 
{ 
    BindingList<Product> products = new BindingList<Product>(); 

    /* 
    * Your code to query the db. 
    */ 

    while reader.Read() 
    { 
    Product existingProduct = new Product(); 

    int id = // value from the reader 
    string name = // value from the reader 
    double version = // value from the reader 

    try 
    { 
     existingProduct = products.Single(p => p.ID == id && p.Name == name); 
     existingProduct.Version.Add(version); 
    } 
    catch // No product yet exists for this id and name. 
    { 
     existingProduct.ID = id; 
     existingProduct.Name = name; 
     existingProduct.Version.Add(version); 
     products.Add(existingProduct); 
    } 
    } 

    return products; 
} 

這將只存儲獨一無二產品及其版本列表。並在表單中顯示ComboBoxColumn中每行的唯一版本列表:

public Form1() 
{ 
    InitializeComponent(); 

    this.Products = YourQueryCall(); 
    this.FillDGView(); 
} 

public BindingList<Product> Products { get; set; } 

public void FillDGView() 
{ 
    DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn(); 
    col1.Name = "Product ID"; 
    col1.ValueType = typeof(int); 
    dataGridView1.Columns.Add(col1); 

    DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn(); 
    col2.Name = "Product Name"; 
    col2.ValueType = typeof(string); 
    dataGridView1.Columns.Add(col2); 

    DataGridViewComboBoxColumn col3 = new DataGridViewComboBoxColumn(); 
    col3.Name = "Version"; 
    col3.ValueType = typeof(double); 
    dataGridView1.Columns.Add(col3); 

    for (int i = 0; i < this.Products.Count; i++) 
    { 
    DataGridViewRow row = (DataGridViewRow)(dataGridView1.Rows[0].Clone()); 

    DataGridViewTextBoxCell textCell = (DataGridViewTextBoxCell)(row.Cells[0]); 
    textCell.ValueType = typeof(int); 
    textCell.Value = this.Products[i].ID; 

    textCell = (DataGridViewTextBoxCell)(row.Cells[1]); 
    textCell.ValueType = typeof(string); 
    textCell.Value = this.Products[i].Name; 

    DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)(row.Cells[2]); 
    comboCell.ValueType = typeof(double); 
    comboCell.DataSource = this.Products[i].Version; 
    comboCell.Value = this.Products[i].Version[0]; 

    dataGridView1.Rows.Add(row); 
    } 
} 

希望這有助於!

相關問題