VB.net Datagridview,使用不同的数据源绑定组合框

时间:2014-11-13 02:47:59

标签: vb.net datagridview combobox datasource

我很难弄清楚如何在我的datagridview中的每个组合框中的每一行中使用数据源绑定项目。
基本上,我有3列,第1行和第2行是文本,第3行是组合框 第一行是question category
第2行是question
第3行是answer for each question

答案因问题而异,这就是答案被设置为组合框的原因。

这是我到目前为止所做的......

For Each question As CRM.Domain.Entities.CampaignDB.CampaignQuestion In questions
    'get question category
    questionCategoryStr = questionCategory.Find(Function(column) column.QuestionCategoryId = question.QuestionCategoryId)
    'filter the answers
    answerStr = answers.FindAll(Function(column) column.CampaignQuestionId = question.CampaignQuestionId)

     Dim dgvcc As New DataGridViewComboBoxCell
     With dgvcc
         .DataSource = answerStr
         .ValueMember = "CampaignAnswerId"
          .DisplayMember = "Answer"
     End With

     'this will only add rows with question category and the question, 
     dgvSurveyQuestions.Rows.Add(questionCategoryStr.CategoryName, question.Question)
     'however, if I do this
     dgvSurveyQuestions.Rows.Add(questionCategoryStr.CategoryName, question.Question, dgvcc)
     'it throws an error "Datagridview Default Error Dialog"
Next

Sample image of the error

1 个答案:

答案 0 :(得分:0)

好吧,好吧,现在我明白了......

 dgvSurveyQuestions.Rows.Add(questionCategoryStr.CategoryName, question.Question)
 dgvSurveyQuestions.Item(2, rowIndex) = dgvcc

rowIndex是一个从零开始的计数器,随着循环的进行递增。