简体   繁体   English

如何在c#asp.net中动态绑定具有3个下拉列表的网格视图?

[英]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#. net gridview,我不希望使用内置控件,而希望在C#中手动绑定数据。 Could some one let me know where to start and how can I use 3 different drop downs to add more filtering? 可以让我知道从哪里开始,如何使用3种不同的下拉菜单添加更多过滤条件?

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 . 您有另一个数据库Teachers ,每个教师都有IdName 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: 在“ Students网格视图的每一行中,您必须具有以下列:

 <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: 如果要让您的老师在SqlDataSource ,请更改以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将下拉列表绑定到ASP.Net MVC C#中的动态生成的变量 - How to bind a drop down list to a dynamically generated variable in ASP.Net MVC C# 如何动态创建下拉列表并在视图中枚举? asp.net MVC 5 - How to dynamically create drop down lists and enumerate them at view? asp.net MVC 5 如何在asp.net c下拉列表中绑定工具提示# - How to bind tool tip in drop down list in asp.net c# 如何使用五元嵌套列表创建和绑定网格以显示C#ASP.net .aspx - How to Create and Bind the Grid for Display With Quintuple Nested Lists C# ASP.net .aspx 如何从动态添加的标签中检索数据,asp.net c#中动态添加的div标签中的下拉列表 - how to retrieve data from dynamically added label,drop down list in dynamically added div tag in asp.net c# C# Asp.net Cascading 下拉选择列表使用 Searchable Options List 插件 - C# Asp.net Cascading drop-down select lists using Searchable Options List plugin C#ASP.NET-下拉列表存储当前值 - C# ASP.NET - Drop Down Lists storing current value 在ASP.NET中将控件动态绑定到网格视图 - Dynamically bind controls to the grid view in asp.NET 如何确定选定的值在asp.net MVC视图中的下拉列表? - How to determine selected value for drop down lists in asp.net MVC View? 在ASP.NET C#中动态填充数据库中的下拉列表 - fill drop down list from database dynamically in asp.net c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM