Click here to Skip to main content
14,978,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to add the row in the grid view on the drop down selected index changed in the grid view
Posted
Comments
Legor 30-Aug-12 4:40am
   
This question has to be answered a thousand times now.

   
my aspx code:
XML
<div>
        <asp:GridView runat="server" ID="grdview">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:DropDownList runat="server" ID="Select_Spin2" AutoPostBack="true" OnSelectedIndexChanged="Select_Spin2_IndexChanged">
                            <asp:ListItem Text="First" Value="1" />
                            <asp:ListItem Text="Second Value="2" />

                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>



My code behind

C#
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);            
            grdview.DataSource = dt;
            grdview.DataBind();
        }
    }


    protected void Select_Spin2_IndexChanged(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Column1");
        DataRow dr = dt.NewRow();
        foreach (GridViewRow gvr in grdview.Rows)
        {
            dr = dt.NewRow();
            dr["Column1"] = ((DropDownList)gvr.Cells[0].FindControl("Select_Spin2")).SelectedValue;
            dt.Rows.Add(dr);
        }
        DataRow dr1 = dt.NewRow();
        dt.Rows.Add(dr1);
        grdview.DataSource = dt;
        grdview.DataBind();
        foreach (GridViewRow row in grdview.Rows)
        {
            DropDownList ddl = row.FindControl("Select_Spin2") as DropDownList;
            ddl.SelectedValue = row.Cells[1].Text;
        }
        

    }
}
   
XML
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
        <Columns>
        <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
        <asp:TemplateField HeaderText="Header 1">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Header 2">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Header 3">
            <ItemTemplate>
                 <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </ItemTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <FooterTemplate>
             <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" />
            </FooterTemplate>
        </asp:TemplateField>
        </Columns>
</asp:gridview>
   
Hiiii I Hope That Help You
DataGridViewTextBoxColumn GRDCol = new DataGridViewTextBoxColumn();
GrdCur_Acc_Cust.Rows.Add(GRDCol);
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900