mahesh  kumar

mahesh kumar

  • NA
  • 20
  • 1.2k

to generate Grid view columns dynamically at run time

Jul 8 2021 2:15 PM

I have a requirement where I need to generate the gridview at run time and load the columns at the run time . Is it possible to generate the equivalent code of the below at run time in code behind file in c#?

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False"  OnDataBound="gv_DataBound"  DataKeyNames="idservicemapping" EmptyDataText="No Records Available." EnableModelValidation="True"  OnRowCancelingEdit="CancelEdit" OnRowDataBound="RowDataBound" OnRowDeleting="gv_RowDeleting" OnRowEditing="Edit" OnRowUpdating="Update" PageSize="8" ShowFooter="false">
           <Columns>
               <asp:TemplateField >
                   <EditItemTemplate>
                       <asp:Label ID="Label1" runat="server" Visible="false"  Text='<%# Eval("idservicemapping") %>'> </asp:Label>
                 </EditItemTemplate>
                 <ItemTemplate>
                     <asp:Label ID="Label1" runat="server" Visible="false"  Text='<%# Bind("idservicemapping") %>'></asp:Label>
                   </ItemTemplate>
                   <FooterTemplate>
                       <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="true" OnClick="LinkButton1_Click">Save</asp:LinkButton>
              </FooterTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Pillar">
              <ItemTemplate>
                  <asp:Label ID="lblPillar" runat="server" Text='<%# Eval("pillar")%>'></asp:Label>
              </ItemTemplate>
              <EditItemTemplate>
                  <asp:DropDownList ID="ddlpillar" runat="server" >
                  </asp:DropDownList>
              </EditItemTemplate>
              <FooterTemplate>
                  <asp:DropDownList ID="ddlpillarinsert" CausesValidation="true" runat="server"> </asp:DropDownList>

              </FooterTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Service Name">
              <ItemTemplate>
                  <asp:Label ID="lblServiceName" runat="server" Text='<%# Eval("ServiceName")%>'></asp:Label>
              </ItemTemplate>
              <EditItemTemplate>
                  <asp:DropDownList ID="ddlServiceName" runat="server" >
                  </asp:DropDownList>
              </EditItemTemplate>
               <FooterTemplate>
                  <asp:DropDownList ID="ddlServiceNameinsert" runat="server"> </asp:DropDownList>

              </FooterTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Service Type">
              <ItemTemplate>
                  <asp:Label ID="lblservicetype" runat="server" Text='<%# Eval("servicetype")%>'></asp:Label>
              </ItemTemplate>
              <EditItemTemplate>
                  <asp:DropDownList ID="ddlservicetype" runat="server">
                  </asp:DropDownList>
              </EditItemTemplate>
              <FooterTemplate>
                  <asp:DropDownList ID="ddlservicetypeinsert" runat="server"> </asp:DropDownList>

              </FooterTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Service Code">
              <ItemTemplate>
                  <asp:Label ID="lblservicecode" runat="server" Text='<%# Eval("serviceCode")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlservicecode" runat="server">
                </asp:DropDownList>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="ddlservicecodeinsert" runat="server"> </asp:DropDownList>

            </FooterTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />

        <asp:CommandField ShowDeleteButton="true" />
    </Columns>
</asp:GridView>

 


Answers (1)