Vishal
Vishal

Reputation: 6378

stop sorting the sheetname from excel in asp.net

I get the sheet Names from excel and create the buttons whose text is the sheet's name.

But every time the buttons are created they are sorted alphabetically. I don't want to sort them.

Here is my code :

    Dim objConn As OleDbConnection = Nothing
    Dim dt As System.Data.DataTable = Nothing

    Try

        Dim connString As String = ""

        If Extension = "xls" Or Extension = ".xls" Then

            connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileUploadPath & sender.text & ";Extended Properties=" + Convert.ToChar(34).ToString() + "Excel 8.0;HDR=No;IMEX=1" + Convert.ToChar(34).ToString() + ""

        ElseIf Extension = "xlsx" Or Extension = ".xlsx" Then

            connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & FileUploadPath & sender.text & ";Extended Properties=" + Convert.ToChar(34).ToString() + "Excel 12.0;HDR=No;IMEX=2" + Convert.ToChar(34).ToString() + ""

        End If

        objConn = New OleDbConnection(connString)

        objConn.Open()

        dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)

        If Not dt Is Nothing Then

            Dim i As Integer = 1

            For Each row As DataRow In dt.Rows
                Dim btn As Button = New Button
                btn.ID = "btnSheet" & i
                btn.Text = row("TABLE_NAME").ToString().Substring(0, row("TABLE_NAME").ToString.Length - 1)
                excelSheetButtonsList.Add(btn)
                tdInfo.Controls.Add(btn)
                tdInfo.Controls.Add(New LiteralControl(" "))
                AddHandler btn.Click, AddressOf ExcelSheetNameButtons_Click
                i += 1
            Next

        End If

    Catch ex As Exception

    Finally

        If objConn IsNot Nothing Then
            objConn.Close()
            objConn.Dispose()
        End If
        If dt IsNot Nothing Then
            dt.Dispose()
        End If

    End Try

The image 1 is the output of the above code :

enter image description here

Now the image 2 describes the excel sheet :

enter image description here

I want my output in the order as it is in image 2.

Upvotes: 1

Views: 241

Answers (1)

Yuriy Galanter
Yuriy Galanter

Reputation: 39777

One (albeit not very reliable) way is to sort resulting DataTable (by creating a DataView based on it) sort it on DateCreated or DateModified columns.

Upvotes: 1

Related Questions