How to add a new row in combobox , after binding it using LINQ. in vb.net [modified]
-
Public Function ReturnProjectStatus() As Object
Dim Baseclassobject As BaseClassDataContext = Nothing
Try
Baseclassobject = New BaseClassDataContext(BLLmdlCommon.strConfiguration)
Dim query = From p In Baseclassobject.GetTable(Of CP_PRJ_ProjectStatus)() Select p
Return query
Catch ex As ExceptionEnd Try End Function
private sub abc()
cmbStatus.DisplayMember = "name"
cmbStatus.ValueMember = "ID"
Dim query = ReturnProjectStatus()combobox1.datasource = query
'Now I want to add a new item in combobox after binding , at first index.How should i do it
end sub</pre>modified on Wednesday, August 20, 2008 3:13 PM
-
Public Function ReturnProjectStatus() As Object
Dim Baseclassobject As BaseClassDataContext = Nothing
Try
Baseclassobject = New BaseClassDataContext(BLLmdlCommon.strConfiguration)
Dim query = From p In Baseclassobject.GetTable(Of CP_PRJ_ProjectStatus)() Select p
Return query
Catch ex As ExceptionEnd Try End Function
private sub abc()
cmbStatus.DisplayMember = "name"
cmbStatus.ValueMember = "ID"
Dim query = ReturnProjectStatus()combobox1.datasource = query
'Now I want to add a new item in combobox after binding , at first index.How should i do it
end sub</pre>modified on Wednesday, August 20, 2008 3:13 PM
Try adding the 'static' items first, then use AppendDataBoundItems = true
'Howard
-
Try adding the 'static' items first, then use AppendDataBoundItems = true
'Howard
I am using windows . Before switched on to LINQ , I was doing like , i fetched the datatable from combo box datasource , Then adding a new row in that table , finally inserting the data in that row , and placing that row in the datatable , and was giving the data source to combo box. In this way i was attaching the items in combo box manually. But this time , using the LINQ , i don't want to use the datatable. Please help.
modified on Thursday, August 21, 2008 9:43 AM
-
I am using windows . Before switched on to LINQ , I was doing like , i fetched the datatable from combo box datasource , Then adding a new row in that table , finally inserting the data in that row , and placing that row in the datatable , and was giving the data source to combo box. In this way i was attaching the items in combo box manually. But this time , using the LINQ , i don't want to use the datatable. Please help.
modified on Thursday, August 21, 2008 9:43 AM
To be able to insert in the code you need to know the type of object in your list. Your original query returns IEnumerable(of CP_PRJ_ProjectStatus) so you'd have to insert a new CP_PRJ_ProjectStatus entry into that (which you probably don't want to do). Secondly the query is returning the whole table when only two values are needed. You could create your own structure or class to hold the ID,Name values and then insert one at the top, in the example below I've used the generic KeyValuePair class since it's there already. I've put ID into Key and Name into Value.
Public Function ReturnProjectStatus() As List(Of KeyValuePair(of String, String))
Dim Baseclassobject As BaseClassDataContext = Nothing
Try
Baseclassobject = New BaseClassDataContext(BLLmdlCommon.strConfiguration)Dim query = From p In Baseclassobject.GetTable(Of CP\_PRJ\_ProjectStatus)() \_ Select New KeyValuePair(of String, string)(p.ID, p.Name) \_ Return query.ToList() Catch ex As Exception End Try
End Function
Private Sub abc()
cmbStatus.DisplayMember = "name"
cmbStatus.ValueMember = "ID"
Dim query = ReturnProjectStatus()
'Insert new value at top
query.Insert(0, New KeyValuePair(of string, string)("ID","Value")
combobox1.datasource = query
End Sub'Howard