我有一個簡單的數據庫(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());
}
}
}
謝謝@tezzo,這是非常接近的一切,我錯過了什麼。 – Randy