J Talati J Talati - 7 months ago 12
Vb.net Question

GridView is above the drop down list. However, i want it the other way around. Drop down list is programatically added

Dynamically creating a drop down list item is automatically adding the item to the bottom of the page. I have a GridView and dropdownlist items. want the drop down items to be displayed above the gridView

<body>
<form id="form1" runat="server">
<div>

</div>

&nbsp;<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Height="273px" Width="450px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
&nbsp;&nbsp;<br />
&nbsp;
<div>
<asp:Button ID="InputPageSubmitButton" runat="server" Text="Submit" Width="149px" />
</div>
</form>
</body>


HTML CODE ABOVE:

VB.net Code Below

Imports System.IO

Public Class WebForm1
Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Load Excel File on Page Load

loadExcel()
If Not IsPostBack Then
For i As Integer = 0 To GridView1.Rows(0).Cells.Count
Dim dd1 As New DropDownList()
dd1.ID = "dd1" + i.ToString()


dd1.Items.Add("Borough")

form1.Controls.Add(dd1)

Next
End If
MsgBox(GridView1.Rows(0).Cells.Count)


End Sub

Public Function loadExcel() 'loadExcel Function to show the file to the gridView.


Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim filePath = Path.Combine(GlobalVariable.savedPath, GlobalVariable.excelFileName)

'Code to Use an Oledb Connection to get data from the Excel File
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filePath & ";Extended Properties='Excel 12.0 Xml;HDR=YES;';")
'Code to Read the Sheet Selected from the Excel Spread Sheet'
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
'-- Extra Code Not Needed which Maps the tables as Columns from the Spreadsheet
'MyCommand.TableMappings.Add("Table", "Net-informations.com")
DtSet = New System.Data.DataSet
MyCommand.Fill(DtSet)
'Populates GridView with the excel Spreadsheet
GridView1.DataSource = DtSet.Tables(0)
'Code Below needed to show the excel to GridView
GridView1.DataBind()
MyConnection.Close()

Return Nothing
End Function
Protected Sub InputPageSubmitButton_Click(sender As Object, e As EventArgs) Handles InputPageSubmitButton.Click
Response.Redirect("OutputSelectPage.aspx", True)
End Sub
End Class

Answer

You can add your series of DropDownLists in a Panel above the GridView. You can also turn off AutoGenerateColumns and declare the GridView columns in markup to control the width of each one:

<form id="form1" runat="server">
    <asp:Panel ID="pnl1" runat="server" />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ... >
        <Columns>
           <asp:BoundField HeaderStyle-Width="84" DataField="ID" HeaderText="ID" />
           <asp:BoundField HeaderStyle-Width="72" DataField="Name" HeaderText="Name" />
           ...
        </Columns>
    ...
    </asp:GridView>

With this code:

If Not IsPostBack Then
    For i As Integer = 0 To GridView1.Columns.Count - 1
        Dim dd1 As New DropDownList()
        dd1.ID = "dd1" + i.ToString()
        dd1.Width = GridView1.Columns(i).HeaderStyle.Width
        dd1.Items.Add("Borough")
        pnl1.Controls.Add(dd1)
    Next
End If