I have an ASP.NET web app that connects to a SQL database and populates several gridviews on one page, showing different sets of data. Two of the columns are drop downs as I want the user to be able to select a different (category and status). The only way I can get the data to display is if I hard code the category and status names in the gridview itself like this:
<asp:TemplateField SortExpression="rank" Visible="true" HeaderText="Area" >
<HeaderStyle HorizontalAlign="Left" CssClass="col_med" />
<ItemTemplate>
<asp:DropDownList id="ddlRank" AutoPostBack="True" OnSelectedIndexChanged="Rank_Change" runat="server" CssClass="col_med"
SelectedValue='<%# Eval("rank") %>' TabIndex='<%# TabIndex %>'>
<asp:ListItem Value=""> - </asp:ListItem>
<asp:ListItem Value="1"> 1</asp:ListItem>
<asp:ListItem Value="2"> 2</asp:ListItem>
<asp:ListItem Value="3"> 3</asp:ListItem>
<asp:ListItem Value="4"> 4</asp:ListItem>
<asp:ListItem Value="5"> 5</asp:ListItem>
<asp:ListItem Value="6"> 6</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
What I want to do is pull the statuses from the database and display them in my gridview using the
OnRowDataBound="OnRowDataBound"
Pulling the data from the database into the dropdownlists like this:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DataTable ddLine = DAL.listLine();
ddLine = resort(ddLine, "line", "desc");
//Find the DropDownList in the Row
DataTable ddlArea = DAL.listArea();
ddlArea = resort(ddlArea, "area", "desc");
}
}
However this is not working and no data displays in my gridview at all when I try to implement OnRowDataBound and change my gridview like this:
<asp:BoundField HeaderText="Area" DataField="rank" />
<asp:TemplateField HeaderText = "Area">
<ItemTemplate>
<asp:Label ID="lblArea" runat="server" Text='<%# Eval("rank") %>' Visible = "false" />
<asp:DropDownList ID="ddlArea" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
Can anyone advise if the above approach is best practices for my scenario and if so, what could be wrong with the code resulting in my data not showing up?
