Dyanamic Grid

aspx Page :-

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="id" ShowHeaderWhenEmpty="True" OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound">
            <Columns>

                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                        <asp:Label ID="lblid" runat="server" Text='<%#Eval("id") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Label ID="lblid2" runat="server" Text='<%#Eval("id") %>'></asp:Label>
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lblname" runat="server" Text='<%#Eval("name") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txteditName" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                    </FooterTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:Label ID="lblcity" runat="server" Text='<%#Bind("city") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DDLeditCity" runat="server">
                        </asp:DropDownList>
                        <asp:Label ID="lbleditCity" runat="server" Text='<%#Bind("city") %>' Visible="false"></asp:Label>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="DDLCity" runat="server"></asp:DropDownList>
                        <br />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DDLCity" Display="Dynamic" ErrorMessage="RequiredFieldValidator" InitialValue="0" ValidationGroup="Vsn"></asp:RequiredFieldValidator>
                    </FooterTemplate>

                </asp:TemplateField>
                <asp:TemplateField HeaderText="manage">
                    <ItemTemplate>
                        <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="edit"/>
                        <asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="update" />
                        <asp:Button ID="btnCan" runat="server" Text="Cancel" CommandName="cancel"/>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:Button ID="Button1" runat="server" Text="Insert" OnClick="Button1_Click" ValidationGroup="Vsn" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>

        </asp:GridView>



cs Page :-

using System.Data;
using System.Data.SqlClient;

public partial class grid : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection("Data Source=DELL;Initial Catalog=Vsn;Persist Security Info=True;User ID=sa;Password=vishnu");
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            bind();
        }

    }


    public void bind()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Stud", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        con.Open();
        TextBox name=GridView1.FooterRow.FindControl("txtname") as TextBox;
        DropDownList city=GridView1.FooterRow.FindControl("DDLCity") as DropDownList ;
         
        SqlCommand  cmd = new SqlCommand("insert into Stud values ('" + name.Text + "','" + city.Text + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
        bind();

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        TextBox Name = (TextBox) row .FindControl("txteditName");
        DropDownList City = row.FindControl("DDLeditCity") as DropDownList;
        Label id = (Label)row.FindControl("lblid2");

        con.Open();
        SqlCommand cmd = new SqlCommand("update Stud set Name='" + Name.Text + "', City='" + City.SelectedItem + "' Where id=" + Convert.ToInt32(id.Text), con);
        cmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        con.Close();
        bind();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());

        con.Open();
        SqlCommand cmd = new SqlCommand("Delete from Stud Where id='" + id + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();
        bind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        // Insert DropDown Value.
        DropDownList cit = GridView1.FooterRow.FindControl("DDLCity") as DropDownList;
        SqlCommand cmd = new SqlCommand("select Distinct City from Stud", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cit.DataSource = ds;
        cit.DataValueField = "City";
        cit.DataBind();
        cit.Items.Insert(0, new ListItem("--Select--","0"));
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType==DataControlRowType.DataRow)
        {
            if((e.Row.RowState & DataControlRowState.Edit)>0)
            {
                GridViewRow row = e.Row;
                DropDownList cit = (DropDownList)row.FindControl("DDLeditCity");
                Label city=row.FindControl("lbleditCity") as Label;

                SqlCommand cmd = new SqlCommand("select Distinct City from Stud", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                cit.DataSource = ds;
                cit.DataValueField = "City";
                cit.DataBind();
                cit.Items.Insert(0, new ListItem("--Select--", "0"));
                cit.Items.FindByText(city.Text).Selected = true;
            }
        }
    }
}