Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8927513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:14:11+00:00 2026-06-15T08:14:11+00:00

I have a Gridview with 3 TemplateFields: 1 with a DropDownList and 2 others

  • 0

I have a Gridview with 3 TemplateFields: 1 with a DropDownList and 2 others with Texboxes.

I managed to get the first empty row in the GridView. Now, i need to add another row on SelectedIndexChanged event of the DropDownList.

How can I achieve this?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:14:12+00:00Added an answer on June 15, 2026 at 8:14 am

    Default.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel runat="server">
                <ContentTemplate>
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                        Width="317px">
                        <Columns>
                            <asp:TemplateField HeaderText="Text 1">
                                <ItemTemplate>
                                    <asp:TextBox runat="server" Visible='<%# Eval("isLast") %>' ID="Text1"></asp:TextBox>
                                    <asp:Label runat="server" Visible='<%# !(bool)Eval("isLast") %>' Text='<%# Eval("text1") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Text 2">
                                <ItemTemplate>
                                    <asp:TextBox runat="server" Visible='<%# Eval("isLast") %>' ID="Text2"></asp:TextBox>
                                    <asp:Label runat="server" Visible='<%# !(bool)Eval("isLast") %>' Text='<%# Eval("text2") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:DropDownList ID='DropDownList' Visible='<%# Eval("isLast") %>' runat="server" 
                                        onselectedindexchanged="DropDownList_SelectedIndexChanged" AutoPostBack="true">
                                        <asp:ListItem Text="Option 1" Value="1" />
                                        <asp:ListItem Text="Option 2" Value="2" />
                                        <asp:ListItem Text="Option 3" Value="3" />
                                    </asp:DropDownList>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        </form>
    </body>
    </html>
    

    Default.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable("table1");
                dt.Columns.Add("ID", Type.GetType("System.Int32"));
                dt.Columns.Add("text1");
                dt.Columns.Add("text2");
                dt.Columns.Add("isLast", Type.GetType("System.Boolean"));
                for (int i = 0; i < 3; i++)
                {
                    dt.Rows.Add(new object[] { 
                        i, 
                        i < 2 ? "text 1 row " + i : string.Empty, 
                        i < 2 ? "text 2 row " + i : string.Empty, 
                        i == 2 });
                }
                GridView1.DataSource = dt;
                GridView1.DataBind();
                Session["dataTable"] = dt; // change to sql data source
            }
        }
        protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            DataTable dt = (DataTable)Session["dataTable"];
            var maxId = dt.AsEnumerable().Max(row => Convert.ToInt32(row["ID"]));
            var dataRow = dt.AsEnumerable().Where(row => (int)row["ID"] == maxId)
                            .Select(row => row)
                            .FirstOrDefault();
            dataRow["text1"] = ((TextBox)GridView1.Rows[GridView1.Rows.Count - 1].FindControl("Text1")).Text;
            dataRow["text2"] = ((TextBox)GridView1.Rows[GridView1.Rows.Count - 1].FindControl("Text2")).Text;
            dataRow["isLast"] = false;
            dt.Rows.Add(new object[] { maxId + 1, string.Empty, string.Empty, true });
            GridView1.DataSource = dt;
            GridView1.DataBind();
            Session["dataTable"] = dt;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a GridView that I need to dynamically add TemplateField elements to. My
I have a Gridview, with textboxes in templatefields, where i dynamically add columns to
I have issue to disable/enable validators for newly add row to gridview which is
I have a Gridview in which i have two templatefields of dropdownlist. I bound
i have a gridview inside a repeater, and a dropdownlist insid the gridview. now
I have a dropdownlist inside the gridview. Now i want when i click on
I have created a ASP.NET WebForms Gridview and I need to access each row
I have a gridview with Template field. I add a checkbox in templatefield .
Well, I have GridView with editable field like 'TemplateField' with DropDownList. My code: <Columns>
I have an ASP.NET GridView that has four columns. The first three are typical

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.