Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am writing code to dynamically add rows to grid view by clicking on the buttons available. The ADD button will add new rows to grid view and the Delete button will delete a row.
I would like to restrict deletion to new rows that were inserted but as by the Add button. Currently, the code allows users to delete the first record populating the grid view and I would like to prevent this.
Can any one give me an idea?

First form which was initially loaded: I am adding text to text box and later I will click on Add

First Image

Second form which will be loaded after clicking ADD:

Second Image

If I click Delete on the red highlighted field, I don't want to delete it, as it is my first record. In this case, I would like delete to remove the later record.

Here is my code:

protected void btndelete_click(object sender, EventArgs e)
    {
        Button lb = (Button)sender;
        GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
        int rowID = gvRow.RowIndex + 1;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 1)
            {
                if (gvRow.RowIndex < dt.Rows.Count - 1)
                {
                    //Remove the Selected Row data
                    dt.Rows.Remove(dt.Rows[rowID]);
                }
            }
            //Store the current data in ViewState for future reference
            ViewState["CurrentTable"] = dt;
            //Re bind the GridView for the updated data
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

   }


Any help is appreciated.
Posted

Well first coming to my mind is;

You might have a column named "flag", "status" or something like that. Make that column visibilty false. At first load of the gridview foreach row in the gridview make this "flag" values as "false". So when the user adds a new column assign the "flag column" value to true. So while deleting the data check this column if it is true or false. This might not be the best answer but it might solve your problem temporary.
 
Share this answer
 
Comments
demouser743 3-Jun-11 9:24am    
Hi i think the code is working fine but when i am deleting the text box which i filled in the first form is getting cleared can you tell how to over come this
Orcun Iyigun 3-Jun-11 12:52pm    
Well as far as I understand the value in the textbox cleared out right. When you delete an autopostback event occurs So because of this the value of the textbox clears. In order to avoid this with an if statement check on page load if it is a Postback or not. Like;
if(!Page.IsPostBack)
{
//assign your textbox value
}

Let me know if I understand it wrong.
Yeah, first things first. Solve the problems temporarily first. One more point which is coming in mind, instead of making the visibility false, delete the row from datable and Call Grid Populating Function....
 
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