1
Answer

vb.net 2010 combo box

Sie Ste

Sie Ste

9y
901
1

 In a VB.NET 2010 desktop application, I am using the following new logic to load values into a combo box.

Public dirAccessFiles As String() = Directory.GetFiles("H:\FilesTest", "*.accdb")
 Try
            Dim dir As String

            For Each dir In dirAccessFiles
                cboAccessFile.Items.Add(Path.GetFileNameWithoutExtension(dir))
            Next
        Catch except As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try

The user wants the file names to be in descending order in the combo box. Thus, can you tell me how to modify the code list above and/or would you show me code on how I can load file names into the combo box in descending order?
Answers (1)
0
Saroj Kumar Sahu

Saroj Kumar Sahu

NA 838 8.4k 9y
Hi,
Please see the below code to bind the combobox in desc order
 
Dim dirAccessFiles As String() = Directory.GetFiles("H:\FilesTest", "*.accdb")
Try
Dim dir As String
' Create new DataTable instance.
Dim dt As New DataTable
Dim dc As DataColumn
Dim row As DataRow
' Create one columns in the DataTable.
dc = New DataColumn("Name", GetType(String))
dt.Columns.Add(dc)
For Each dir In dirAccessFiles
'cboAccessFile.Items.Add(Path.GetFileNameWithoutExtension(dir))
row = dt.NewRow
' Add rows in the DataTable.
row.Item(0) = Path.GetFileNameWithoutExtension(dir)
dt.Rows.Add(row)
Next
'sort the datatable in desc order
dt.DefaultView.Sort = "Name DESC"
cboAccessFile.DataSource = dt
cboAccessFile.DisplayMember = "Name"
cboAccessFile.ValueMember = "Name"
Catch except As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
Accepted