简体   繁体   English

当GridView的标头未知时,如何在GridView的每个单元中都有DropDownList

[英]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.

相关问题 将DropDownList添加到gridview单元格 - Add DropDownList to a gridview cell 我可以在gridview中有一个用于数据输入的DisplayName和列标题吗? - Can I have one DisplayName for data entry and the Column Headers in a gridview? 编辑Gridview时如何使用DropdownList - How to use DropdownList when editing Gridview 当单击Gridview中的单元格时,然后更改DropDownList中的选定索引 - When click cell in Gridview then change selected index in DropDownList 如何在GridView中的每个单元格周围放置边框? - How to put a border around each cell in GridView? 如何基于下拉列表的选择动态设置网格视图中单元格的值? - how to dynamically set the value of a cell in gridview based on selection of dropdownlist? 除了文本框之外,如何在编辑时读取gridview单元格? - How can you read a gridview cell when editing besides textboxes? 当我们从gridview选中的行中有相应的datatextfield时,如何显示下拉列表中的项目被选中 - How to show item in dropdownlist as being selected when we have corresponding datatextfield from gridview selected row 如何为每个gridview行定义一个javascript数组? - how can I define a javascript array per every gridview row? 如何检查GridView上每一行的列值是否相同? - How can I check if column value same on every row on GridView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM