Archana B.R
Archana B.R

Reputation: 407

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. I want DropDownList in each and every cell of my GridView as shown in the attached image.

According to the image: The values in the DropDownList under each header are = {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. Similarly I want this event to happen at once for all the values that I have selected in GridView when I hit SAVE.

Therefore my Gridview is just a way to provide input, not bound to any datasource.

How do I achieve it. Any help would be useful. Thank you.

enter image description here

Upvotes: 2

Views: 318

Answers (2)

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

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

    }

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460340

You can use TemplateFields for the 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>

Upvotes: 0

Related Questions