Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi ,

am adding rows to the gridview without using a database like below code
how can i delete the row from gridview
C#
public static DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
            dt.Columns.Add(new DataColumn("Column1", typeof(string)));
            dt.Columns.Add(new DataColumn("Column2", typeof(string)));
            dr = dt.NewRow();
            dr["RowNumber"] = 1;
            dr["Column1"] = "column1cell";
            dr["Column2"] = "column2cell";
            dt.Rows.Add(dr);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }



C#
protected void Button1_Click(object sender, EventArgs e)
    {
        if (dt.Rows.Count > 0)
        {
            dt.Rows.RemoveAt(0);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
Posted
Comments
Member 8635497 31-May-12 3:39am    
A field or property with the name 'RowNumber' was not found on the selected data source. how to remove it
Member 13543270 21-Feb-18 10:54am    
some of the rows in the gridview don't delete while others do

http://www.codersource.net/asp-net/asp-net-2-0/grid-view-control-in-asp-net-2-0.aspx[^]

Follow this link and see how to set the EDITING, DELETING in datagridview........:)
 
Share this answer
 
hi ,
i have made the selected row of the grid view visible=false
used session for this

DataTable table = (DataTable)Session["CurrentData"];
       for (int i = GridView1.Rows.Count - 1; i >= 0; i--)
       {
           CheckBox chkbox = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("selector");
           if (chkbox.Checked)
           {
               GridView1.Rows[i].Visible = false;
           }

       }


here the selector is the gridview checkbox id
 
Share this answer
 
v2
DataTable table = (DataTable)Session["CurrentData"];

for (int i = GridView1.Rows.Count - 1; i >= 0; i--)
        {

            CheckBox chkbox = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("selector");
            if (chkbox.Checked)
            {
                // GridView1.Rows[i].Visible = false;

                myDt.Rows.RemoveAt(i);
                DataTable myDt1 = (DataTable)Session["tabel"];
                GridView1.DataSource = myDt1;
                GridView1.DataBind();


            }
        }
 
Share this answer
 
Comments
Member 13564472 28-Jan-18 22:30pm    
what is mydt..??
Hi,

Try this code it is working

protected void grdDaily_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
LinkButton lb = (LinkButton)e.Row.FindControl("LB_Remove");
if (lb != null)
{
if (dt.Rows.Count > 1)
{
if (e.Row.RowIndex == dt.Rows.Count - 1)
{
lb.Visible = false;
}
}
else
{
lb.Visible = false;
}
}
}
}
 
Share this answer
 

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