Click here to Skip to main content
12,998,654 members (129,177 online)
Rate this:
 
Please Sign up or sign in to 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
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();
        }
    }



protected void Button1_Click(object sender, EventArgs e)
    {
        if (dt.Rows.Count > 0)
        {
            dt.Rows.RemoveAt(0);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
Posted 8-Dec-11 3:12am
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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........:)
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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();
 

            }
        }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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;
}
}
}
}
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 15 Dec 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100