将组合框添加到datagridview C#Winforms
问题描述:
我想将combobox列添加到datagridview。
我使用这个代码绑定我的访问数据库的数据。
I want to add combobox column to datagridview. I use this code to bind data from my access database.
Class1.Connection.Open();
oleCommand = new OleDbCommand("SELECT tbAuditDetails.AuditNo, tbAuditQuestions.AutoSubcontent, tbAuditQuestions.AutoID, tbAuditQuestions.Questions, tbScore.Description, tbAuditDetails.QuestionID, tbAuditQuestions.QuestAutoID, tbAuditDetails.ScoreID, tbScore.Score, tbAuditQuestions.SubContentID, tbAuditDetails.ProfileID FROM ((tbAuditDetails INNER JOIN tbAuditQuestions ON tbAuditDetails.QuestionID = tbAuditQuestions.QuestionID) INNER JOIN tbScore ON tbAuditDetails.ScoreID = tbScore.ScoreID) WHERE (([tbAuditDetails.AuditNo] = " + Class1.detailsauditno + ") AND ([tbAuditQuestions.AutoSubcontent] = '" + newautosubcontentid + "') AND ([tbAuditDetails.ProfileID] = " + proid + ")) ORDER BY [tbAuditQuestions.QuestAutoID], [tbAuditDetails.QuestionID]", Class1.Connection);
oleAdapter = new OleDbDataAdapter(oleCommand);
oleBuilder = new OleDbCommandBuilder(oleAdapter);
oleDs = new DataSet();
oleAdapter.Fill(oleDs, "tbAuditDetails");
oleTable = oleDs.Tables["tbAuditDetails"];
Class1.Connection.Close();
dataGridView1.DataSource = oleDs.Tables["tbAuditDetails"];
我想将一列变成combobox,所以我可以选择值。此组合框值来自另一个表,它称为tbscore(id,说明)。
I want change one column into combobox, so i can choose the value. This combobox value come from another table, it called "tbscore" (id, description).
tbScore.Description
tbScore.Description
有没有人知道如何实现?非常感谢。
谢谢。
Does anyone know how to achieve this? Really appreciated. Thank you.
答
private void AddComboboxColumn()
{
DataGridViewComboBoxColumn ColComboBox = new DataGridViewComboBoxColumn();
dataGridView1.Columns.Add(ColComboBox );
ColComboBox.DataPropertyName = "ScoreID";
ColComboBox.HeaderText = "Category";
ColComboBox.ValueType = typeof(string);
ColComboBox.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
ColComboBox.DisplayIndex = 2;
ColComboBox.Width = 150;
ColComboBox.DataSource = oleDs ;
ColComboBox.DisplayMember = "description";
ColComboBox.ValueMember = "ScoreID";
ColComboBox.Name = "ScoreID";
ColComboBox.DataPropertyName = "ScoreID";
}
您可以在代码之后调用此函数
You can call this function after your code