[英]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
,每个教师都有Id
和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: 在“ 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.