当GridView的标题为Unknown时,如何在GridView的每个单元格中都有DropDownList

时间:2012-08-27 08:49:18

标签: c# asp.net

我有GridView,每次运行程序时GridView的标题都会发生变化。我想在DropDownList的每个单元格中GridView,如附图所示。

根据图像: 每个标题下的DropDownList中的值为= {1,2,3,4,5,6,7,8,9,10}。

假设我从KITCHEN2的DropDownList中选择值2,当我点击SAVE时,我想要2个灯(Lamp1,Lamp2)(如果我在GridView的第一列中选​​择了Lamp_profile)要更新在Kitchen2的数据库中。同样地,我希望当我点击SAVE时,我在GridView中选择的所有值都会立即发生此事件。

因此我的Gridview只是一种提供输入的方式,而不是绑定到任何数据源。

我如何实现它。任何帮助都会有用。谢谢。

enter image description here

2 个答案:

答案 0 :(得分:1)

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");
              ....
        }

    }

答案 1 :(得分:0)

您可以将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>