簡體   English   中英

來自一個單元格為組合框的數據庫的Datagridview數據填充C#

[英]Datagridview Data Population from the database where one cell is a combobox c#

我有一個datagrid視圖,需要用數據庫中的內容填充。 我的數據庫內容在一個數據表中,通常如下所示:

產品編號ProdName版本

1 abc 1.1

1 abc 2.1

1 abc 3.1

2防御3.1

2防御3.2

3 ghi 1.1

4 jkl 1.1

4 jkl 1.2

現在我的問題是,當我在datagrid視圖中顯示內容時,我希望它按原樣顯示,其中version應該是下拉的comboboxcell,所以每個產品都有一個version列表:

產品編號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

本質上,您需要對查詢到的數據進行排序,用每個版本的所有列表保存每個唯一的產品。 然后,您將為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);
  }
}

希望這可以幫助!

暫無
暫無

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

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