Program to bind data to a windows ComboBox with the condition that the DisplayId's will be in sequential order using VB.net.

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 881
Dim dt = GetData()

Dim bs As New BindingSource()

Dim lstDS = dt.AsEnumerable().[Select](Function(dr) InlineAssignHelper(ModifiedImageID, Enumerable.Range(1, dt.Rows.Count)), InlineAssignHelper(ImageData, dr.Field(Of String)("ImageData"))).ToList()

bs.DataSource = lstDS

comboBox1.DataSource = bs.DataSource

comboBox1.DisplayMember = "ModifiedImageID"

comboBox1.ValueMember = "ImageData"


and the GetData() is defined as

Public Function GetData() As DataTable


Dim dt As New DataTable()



'Add some columns

dt.Columns.Add(New DataColumn("ImageID", GetType(Int32)))

dt.Columns.Add(New DataColumn("ImageData", GetType(String)))



'Add some rows



'First Row

Dim row1 As DataRow = dt.NewRow()

row1("ImageID") = 1

row1("ImageData") = "ImageData1"



'Second Row

Dim row2 As DataRow = dt.NewRow()

row2("ImageID") = 11

row2("ImageData") = "ImageData11"



'Third Row

Dim row3 As DataRow = dt.NewRow()

row3("ImageID") = 12

row3("ImageData") = "ImageData12"



'Fourth Row

Dim row4 As DataRow = dt.NewRow()

row4("ImageID") = 15

row4("ImageData") = "ImageData15"



'Fifth Row

Dim row5 As DataRow = dt.NewRow()

row5("ImageID") = 18

row5("ImageData") = "ImageData18"



'Sixth Row

Dim row6 As DataRow = dt.NewRow()

row6("ImageID") = 19

row6("ImageData") = "ImageData19"



'Seventh Row

Dim row7 As DataRow = dt.NewRow()

row7("ImageID") = 21

row7("ImageData") = "ImageData21"



'Add the rows to the table

dt.Rows.Add(row1)

dt.Rows.Add(row2)

dt.Rows.Add(row3)

dt.Rows.Add(row4)

dt.Rows.Add(row5)

dt.Rows.Add(row6)

dt.Rows.Add(row7)



'return the table

Return dt

End Function

Comments or Responses

Login to post response