2016-12-15 50 views
0

我是ASP MVC的新用戶。我想將DropDownList中的數據庫中的數據加載到gridview中。但是數據無法加載。ASP,DropDownList網格視圖

HTML CODE

   <asp:BoundField DataField="id" HeaderText="Code"> 
       <HeaderStyle Width="106px" HorizontalAlign="center" BorderColor="White" BorderWidth="1" /> 
       <ItemStyle Width="102px" HorizontalAlign="center" BorderWidth="1px" /> 
      </asp:BoundField> 
      <asp:BoundField DataField="name" HeaderText="Nom Cours"> 
       <HeaderStyle Width="300px" HorizontalAlign="left" BorderColor="White" BorderWidth="1" /> 
       <ItemStyle Width="300px" HorizontalAlign="left" BorderWidth="1px" /> 
      </asp:BoundField> 



      <asp:templatefield HeaderText="Prix/Heure (HTG)"> 
      <HeaderStyle Width="160px" HorizontalAlign="center" BorderColor="White" BorderWidth="1" /> 
      <ItemStyle Width="160px" HorizontalAlign="center" BorderWidth="1px" /> 
       <itemtemplate>  
        <asp:DropDownList ID="ddlprice" Width="160px" HorizontalAlign="center" runat="server"> 

        </asp:DropDownList>       
       </itemtemplate> 
      </asp:templatefield>   


      <asp:templatefield HeaderText="Sélectionner"> 
      <HeaderStyle Width="300px" HorizontalAlign="center" BorderColor="White" BorderWidth="1" /> 
        <ItemStyle Width="300px" HorizontalAlign="center" BorderWidth="1px" /> 
       <itemtemplate> 
      <asp:checkbox ID="cbSelect" CssClass="gridCB" runat="server" HorizontalAlign="center"></asp:checkbox>     
       </itemtemplate> 
      </asp:templatefield> 
     </Columns> 
     <FooterStyle BackColor="Tan" Height="30px" HorizontalAlign="Center" /> 
     <HeaderStyle BackColor="Navy" Font-Bold="True" Height="22px" HorizontalAlign="Left" 
      ForeColor="WhiteSmoke" BorderColor="Navy" VerticalAlign="Top" BorderWidth="2px" Width="910px" Font-Size="Small" /> 
     <PagerSettings Mode="NumericFirstLast" /> 
     <PagerStyle BackColor="SkyBlue" ForeColor="WhiteSmoke" 
      HorizontalAlign="Center" /> 
     <RowStyle Height="5px" Font-Size="Smaller" /> 
     <SelectedRowStyle BackColor="Aquamarine" ForeColor="GhostWhite" BorderColor="Silver" 
      BorderStyle="None" /> 
     <SortedAscendingCellStyle BackColor="SkyBlue" /> 
     <SortedAscendingHeaderStyle BackColor="#DAC09E" /> 
     <SortedDescendingCellStyle BackColor="#E1DB9C" /> 
     <SortedDescendingHeaderStyle BackColor="#C2A47B" /> 
    </asp:GridView> 
</div> 

C#代碼

protected void Page_Load(object sender, EventArgs e) 
    { 

     if (!Page.IsPostBack) 
     { 
      ddlClassroom.SelectedValue = "-1"; 

      BindDataGridClass(); 
      BindDataGridCourse(); 
      loadListPrice(); 
     } 

private void loadListPrice() 
    { 
     List<Course> listPrice = Course.getListCoursePriceOrdered(); 
     ddlprice.DataValueField = "id"; 
     ddlprice.DataTextField = "price"; 
     ddlprice.DataSource = listPrice; 
     ddlprice.DataBind(); 
     //ddlprice.Items.Insert(0, new DropDownListItem("--Selectionner--", "-1")); 
     ddlprice.SelectedValue = "0"; 

    } 
+0

請添加代碼的正確描述。 – Jay

+0

這是所有的網頁表單代碼,與asp.net-mvc無關 –

回答

0

首先OnRowDataBound事件中的代碼添加到GridView

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"> 

那麼後面

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //check if the row is a datarow 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     //find the dropdownlist in the row with findcontrol and cast it back to one 
     DropDownList ddlprice = e.Row.FindControl("ddlprice") as DropDownList; 

     //you now have access to all the dropdownlist properties 
     ddlprice.DataSource = Course.getListCoursePriceOrdered(); 
     ddlprice.DataValueField = "id"; 
     ddlprice.DataTextField = "price"; 
     ddlprice.DataBind(); 
     ddlprice.SelectedValue = "0"; 
    } 
}