[英]How can I have DropDownList in each and every cell of the GridView when the headers of the GridView are Unknown
I have a GridView
, and The headers of the GridView
changes every time I run my program. 我有一个
GridView
,并且每次运行程序时GridView
的标题都会更改。 I want DropDownList
in each and every cell of my GridView
as shown in the attached image. 我希望在
GridView
每个单元格中都具有DropDownList
,如所附图像所示。
According to the image: The values in the DropDownList
under each header are = {1,2,3,4,5,6,7,8,9,10}. 根据图像:在每个标题下的
DropDownList
的值为= {1,2,3,4,5,6,7,8,9,10}。
Suppose I select value 2 from the DropDownList
for KITCHEN2, When I hit on SAVE, I want 2 Lamps(Lamp1, Lamp2)(provided I have selected Lamp_profile in the first column of my GridView) to be updated in the database for Kitchen2. 假设我从KITCHEN2的
DropDownList
选择了值2,当我点击SAVE时,我想在Kitchen2的数据库中更新2 Lamps(Lamp1,Lamp2)(前提是我在GridView的第一列中选择了Lamp_profile)。 Similarly I want this event to happen at once for all the values that I have selected in GridView
when I hit SAVE. 同样,当我单击SAVE时,我希望针对在
GridView
选择的所有值立即发生此事件。
Therefore my Gridview
is just a way to provide input, not bound to any datasource. 因此,我的
Gridview
只是提供输入的一种方式,而不是绑定到任何数据源。
How do I achieve it. 我该如何实现。 Any help would be useful.
任何帮助将是有用的。 Thank you.
谢谢。
You can try with TemplateField
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kitchen1">
<ItemTemplate>
<asp:DropDownList ID="Kitchen1_DropDownList" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kitchen2">
<ItemTemplate>
<asp:DropDownList ID="Kitchen2_DropDownList" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kitchen3">
<ItemTemplate>
<asp:DropDownList ID="Kitchen3_DropDownList" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kitchen4">
<ItemTemplate>
<asp:DropDownList ID="Kitchen4_DropDownList" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You must know how bind your data in code behind (You can use Eval.DataBinder)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
DropDownList Kitchen1DropDownList = (DropDownList)e.Row.FindControl("Kitchen1_DropDownList");
....
}
}
You can use TemplateFields
for the DropDownLists. 您可以将
TemplateFields
用于DropDownLists。
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" >
<Columns>
<asp:TemplateField HeaderText="Col1">
<ItemTemplate>
<asp:DropDownList ID="Ddl1" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Col2">
<ItemTemplate>
<asp:DropDownList ID="Ddl2" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Col3">
<ItemTemplate>
<asp:DropDownList ID="Ddl3" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.