2013-06-27 32 views
1

我有一個簡單的數據庫(SQL)汽車表有六個字段:ID,所有者,年份,顏色,品牌和型號。顏色表只是:ID,顏色名稱。DataGridView將TextBoxColumn轉換爲ComboBoxColumn

我有一個DataGridView窗體。當我將我的汽車表綁定到DataGridView時,一切都起作用。

現在,我試圖讓DataGridView中的顏色列成爲一個組合框,用於編輯並僅列出顏色表中的顏色。我可以創建並添加一個組合框到DataGridView,但我想'轉換'已經有數據的列。

在此先感謝您提供的任何幫助。

using System.Collections.Generic; 
using System.Linq; 
using System.Windows.Forms; 

namespace EditingCells 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

      var myDataBase = new CarsEntities(); 
      List<MyCar> data = myDataBase.MyCars.OrderBy(o => o.Owner).ToList(); 

      //dataGridView1.AutoGenerateColumns = false; 
      dataGridView1.DataSource = data; 

      const int index = 2; // Column Index for Color 

      List<string> myColor = myDataBase.Colors.Select(s => s.Color1).ToList(); 

      var comboBoxColumn = new DataGridViewComboBoxColumn 
      { 
       DataSource = myColor, 
       DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing, 
       AutoComplete = true, 
      }; 
     } 

     private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 
      MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].EditType.ToString()); 
     } 
    } 
} 

回答

1

您必須使用此設置顏色表的數據填充DataGridViewComboBoxColumn:

+0

謝謝@tezzo,這是非常接近的一切,我錯過了什麼。 – Randy

1

在此基礎上 - >「Change Cell Type on Databound DataGridView to Custom DataGridViewCell Type」的主題,我建議你的東西的工作類似如下:

public partial class Form1 : Form 
{ 
    List<string> availableCarColors = new List<string>(); 
    List<MyCar> data = new List<MyCar>(); 

    public Form1() 
    { 
     InitializeComponent(); 

     availableCarColors.Add("Red"); 
     availableCarColors.Add("Blue"); 

     data.Add(new MyCar("Lexus", "Blue")); 
     data.Add(new MyCar("BMW", "Red")); 

     DataGridViewTextBoxColumn colModel = new DataGridViewTextBoxColumn(); 
     colModel.Name = "Model";    //Header Text 
     colModel.DataPropertyName = "Model"; //Property Name of my car class 

     DataGridViewComboBoxColumn colColor = new DataGridViewComboBoxColumn(); 
     colColor.Name = "Color"; 
     colColor.DataPropertyName = "Color"; 
     colColor.DataSource = availableCarColors; 

     dataGridView1.AutoGenerateColumns = false; 
     dataGridView1.Columns.AddRange(new DataGridViewColumn[] {colModel, colColor}); 
     dataGridView1.DataSource = data; 
    } 

    class MyCar 
    { 
     public string Model { get; set; } 
     public string Color { get; set; } 

     public MyCar(string model, string color) 
     { 
      Model = model; 
      Color = color; 
     } 
    } 
} 
0

我只是想後我如何使用@ Tezzo的解決方案來獲得我需要的結果:

using System.Collections.Generic; 
using System.Linq; 
using System.Windows.Forms; 

namespace EditingCells 
{ 
    public partial class Form1 : Form 
    { 
     private readonly CarsEntities _myDataBase = new CarsEntities(); 

     public Form1() 
     { 
      InitializeComponent(); 

      List<MyCar> myData = _myDataBase.MyCars.ToList(); 

      // Yes, you have do make Columns manually. 
      dataGridView1.AutoGenerateColumns = false; 
      dataGridView1.DataSource = myData; 

      List<Color> myColors = _myDataBase.Colors.ToList(); 

      var carOwner = new DataGridViewTextBoxColumn 
       { 
        DataPropertyName = "Owner", 
        HeaderText = @"Owner", 
        Name = "Owner", 
       }; 
      dataGridView1.Columns.Add(carOwner); 

      var carColor = new DataGridViewComboBoxColumn 
       { 
        // Field Name where existing data is stored 
        DataPropertyName = "CarColor", 
        HeaderText = @"Color", 
        Name = "Color", 
        DataSource = myColors, 
        DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing, 
        AutoComplete = true, 

        // Field value from DataSource to store in Car.Color field 
        ValueMember = "ColorName", 

        // Field value from DataSource to show in DataGridView Column 
        DisplayMember = "ColorName", 
       }; 
      dataGridView1.Columns.Add(carColor); 
     } 

     private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
     { 
      _myDataBase.SaveChanges(); 
     } 
    } 
} 
相關問題