将组合框添加到datagridview C#Winforms

时间:2016-04-06 06:40:08

标签: c# database winforms datagridview combobox

我想将combobox列添加到datagridview。 我使用此代码绑定来自访问数据库的数据。

            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"];

我想将一列更改为组合框,因此我可以选择该值。这个组合框的值来自另一个表,它称为" tbscore" (id,description)。

  

tbScore.Description

有谁知道如何实现这一目标?非常感谢。 谢谢。

1 个答案:

答案 0 :(得分:0)

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";
}

您可以在代码

之后调用此函数