Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends
i have a gridview like this
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"
                       onclick="ButtonAdd_Click" />
               </FooterTemplate>
           </asp:TemplateField>
           </Columns>
       </asp:gridview>


i am adding row into gridview in following way
adding one row initially in it having textboxes

on button click calling this function
private void AddNewRowToGrid()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;

                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }


 private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                    box1.Text = dt.Rows[i]["Column1"].ToString();
                    box2.Text = dt.Rows[i]["Column2"].ToString();
                    box3.Text = dt.Rows[i]["Column3"].ToString();

                    rowIndex++;
                }
            }
        }
    }

but i am not getting how to remove row from same
Posted
Updated 31-Jul-11 22:51pm
v2

1 solution

add a templatefield that holds a delete button.
In the onclick event you have to find the row in which the button was clicked.

protected void btnDelete_OnClick(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gridRow = (GridViewRow)btn.NamingContainer;

//you know have the good gridviewrow in which the button was clicked.
// Then you can do
string MyKeyValue = gridRow.Cells[0].Text;

//with the MyKeyValue you have a value which might be the unique key on which you 
//can delete the row


}
 
Share this answer
 
Comments
mayur csharp G 1-Aug-11 5:05am    
i just add one thing in your code to get row index
Dim index As Integer = gridRow.RowIndex

thanks

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