2016-04-06 214 views
0

我想要將combobox列添加到datagridview。 我使用此代碼綁定來自我的訪問數據庫的數據。將組合框添加到datagridview C#Winforms

  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

有誰知道如何實現這一目標?非常感謝。 謝謝。

回答

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

您可以將您的代碼之後調用此函數

相關問題