简体   繁体   中英

VB.net Datagridview, bind combobox with different datasources

I'm having a hard time figuring out how to bind items with datasource in each row in each combobox in my datagridview.
Basically, I have 3 columns, 1st and 2nd rows are texts while the third row is a combobox.
1st row is the question category
2nd row is the question
3rd row is the answer for each question

the answer varies per question, that's why the answer is set as a combobox.

This is what I've got so far..

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

错误的样本图片

lol okay now I get it..

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

rowIndex is a counter that starts from zero and increments as the loop progresses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM