Tuesday, 15 May 2012

vb.net - Binding data to a combobox and add a select option -


I am using VB.NET and Visual Studio 2010

I have a windows form A combobox

I do not fill up using was told the following

  list as dim objSizes (the ASME_Hub_Sizes) = ASME_Hub_Sizes.GetAllHubSizes () if objSizes has nothing to do with me Then it is. cboSize .DisplayMember = "size" .ValueMember = "id" .DataSource = objSizes end to end with   

This works fine, but I want to add select " Do shape ... "option but I am unsure how to do it.

It seems so easy to do this in ASP.net, but it has astounded me

thanks Mick

You can try to add custom objSize objects to the objSize compilation in which ID = 0 and value = "select the shape ..." as it ID 0 is should at the top (I think) and will not conflict with any value in your database, when you save the record, you should avoid typing the selection size. You can validate the combo box for the ... "Object T o Your database will have a little code to me and see that this will work ...

OK, I had another look. As I've suggested, you can, but you have to sort the list before going to COombox. Here is my example:

  Public Class Form1 Private Sub Form1_Load (object, EventArgs e as in) Me.ComboBox1 .DisplayMember = "details" .ValueMember = "ID" with Me.Load handle as .DataSource = sub end class ends with GetListOfObjects.returnListOFObjects end public class dummyObjectForCombo integer public property id public property Description string public sub new (ByVal ID integer, ByVal description string) _ Id = id _description = on Nn End Sub End Class obj1 as public class GetListOfObjects Public sharing function returnListOFObjects () list (dummyObjectForCombo of) obj0 as dim col new list (of dummyObjectForCombo) Dim New dummyObjectForCombo (-1, "Herp") Dim New dummyObjectForCombo (1, the ID according to "Jamie") as the new dummyObjectForCombo in as dim Obj2 (2, "Bob") col.Add (obj1) col.Add (obj2) col.Add (obj0) ' Using a lambda to solve on the basis http://stackoverflow.com/questions/3309188/ C-net-how-sort-a-list-t-by-a-property-in-the-obs Crop returns col.OrderBy (function (x) x.ID) Kolist End Function End Class   

I've used as the highest record instead of 1 -1.

Then if you receive your list as usual, add additional dummy record, then specify it as your combo box data source before sorting it according to the above code.

No comments:

Post a Comment