2012-10-24 83 views
0

我一直有一些麻煩加載CheckBoxList數據。我一直在DisplayMember和ValueMember上出現一些錯誤。CheckBoxList數據綁定錯誤

這是我的鱈魚:。

Protected Sub LoadCBMembers(ByVal objDeptGUID As String) 
    Try 
     Dim myCBTable As New DataTable() 
     With myCBTable.Columns 
      .Add("DisplayName", GetType(String)) 
      .Add("TextValue", GetType(String)) '<<<< change the type of this column to what you actually need instead of integer. 
     End With 
     Dim lqMembers As New lqFireFighterConnectDataContext 
     Dim GetMembers = From r In lqMembers.tbUsers 
         Where r.DeptGUID = objDeptGUID And r.TextFLag = True 
         Select r 
     If GetMembers.Count = 0 Then 
      Exit Sub 
     End If 
     For Each foundMember In GetMembers 
      myCBTable.Rows.Add(foundMember.FirstName & " " & foundMember.LastName, foundMember.CellPhone & "@" & foundMember.PhoneCarriers) 
      ' cbMembers.Items.Add(foundMember.FirstName & " " & foundMember.LastName) 
     Next 
     With cbMembers 
      .DataSource = myCBTable 
      .DisplayMember = "DisplayName" 
      .ValueMember = "TextValue" 
     End With 

    Catch ex As Exception 

    End Try 
End Sub 
+0

你知道你的代碼沒有'cbMembers.DataBind'調用,不是嗎?另外,你說你得到錯誤:哪些錯誤? –

回答

0

試試這個:

With cbMembers 
     .DataSource = myCBTable 
     .DisplayMember = "DisplayName" 
     .ValueMember = "TextValue" 
     .DataBind() 
    End With 

按照documentation,這種方法必須顯式調用。並非所有的控件都需要明確地調用DataBind()(對於很多情況它會隱式調用),但CheckBoxList是個例外。

+0

感謝您的信息並指引我朝着正確的方向前進。 – Ghsotwalker42

+0

謝謝你的幫助,並指出我在正確的方向。我做了一個小小的週末,它工作。 – Ghsotwalker42

+0

在這裏,我必須做的tweek。使用cbMembers .DataSource = myCBTable .DataTextField =「DisplayName」 .DataValueField =「TextValue」 .DataBind() End With – Ghsotwalker42