简体   繁体   中英

How would I dynamically bind a grid view with 3 drop down lists in c# asp.net?

net gridview and I do not wish to use the built in controls and want to bind the data manually in c#. Could some one let me know where to start and how can I use 3 different drop downs to add more filtering?

asp:GridView ID="GridView2" runat="server"  AutoGenerateColumns="False"  OnRowDataBound="gvContactorRowDataBound"  Gridlines="Vertical" >                  
                 <Columns >                         
                     <asp:TemplateField HeaderText="Full Name" SortExpression="contactname" HeaderStyle-BackColor="deepskyblue">
                         <EditItemTemplate>
                             <asp:TextBox ID="txtcontactname2" runat="server" Text='<%# Bind("contactname") %>'></asp:TextBox>
                             <asp:RequiredFieldValidator ID="RequiredFieldValidatorcontactname2" runat="server" ErrorMessage="Full Name is required for contractor update!" Text="*" ForeColor="Red" ControlToValidate="txtcontactname2" display="none"></asp:RequiredFieldValidator>
                         </EditItemTemplate>
                         <ItemTemplate>
                             <asp:Label ID="lblcontactname2" runat="server" Text='<%# Bind("contactname") %>'></asp:Label>
                         </ItemTemplate>
                     </asp:TemplateField>

                     <asp:BoundField DataField="phone" HeaderText="Phone" SortExpression="phone" HeaderStyle-BackColor="deepskyblue"/>
                     <asp:BoundField DataField="email" HeaderText="Email" SortExpression="email" HeaderStyle-BackColor="deepskyblue"/>

You did not mentioned your scenario, therefore i assume that you want show all of students in grid view and in each row have a Drop down to select teacher. You have another data base Teachers and each teacher has Id and Name . So in your code behind you gathered all of the teachers:

protected List<teacher> teachers; 

In each row of Students gridview you must have this column:

 <asp:TemplateField HeaderText="Teacher">
  <EditItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server" CssClass="selectClip" DataSource="<%# teachers %>"
           DataTextField="Name" DataValueField="Id" AppendDataBoundItems="true">
        <asp:ListItem Text="<---Select Teacher--->"></asp:ListItem>
    </asp:DropDownList>
  </EditItemTemplate>
  <ItemTemplate>
      <asp:Label ID="lblShowTeacher" runat="server" Text='<%# Bind("Teacher") %>' />
  </ItemTemplate>
</asp:TemplateField>

If you want to have your teachers in a SqlDataSource , change this:

DataSource="<%# teachers %> to this: DataSourceId="teachersDataSourceId"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM