简体   繁体   中英

Delete row from datatable

I want to delete rows from my dataset. Below is the code...

When the checkbox is selected a value is getting stored as a hiddenfield value

The code is not firing. The values are not getting added to the "rowstodelete".

 protected void BtnRmvFile_Click(object sender, EventArgs e)
    {
string[] IDs = hdnFldSelectedValuesAp.Value.Trim().Split('|');


            //Code for adding items
            foreach (string Item in IDs)
            {
                try
                {
                    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];

                    ViewState["CurrentTable"] = dtCurrentTable;

                    List<DataRow> rowsToDelete = new List<DataRow>();
                    foreach (DataRow row in dtCurrentTable.Rows)
                    {
                        if ((row["FileMasterID"] == Item))
                        {
                            rowsToDelete.Add(row);
                        }
                    }
                    foreach (DataRow row in rowsToDelete)
                    {
                        row.Delete();
                    }
                    dtCurrentTable.AcceptChanges();

                }

If you say the code is not firing i suggest to start by checking with some kind of pop up that your method(BtnRmvFile_Click) is responding. Other than that I don't know if its just the way you pasted the code but make sure the brackets are organized.

If the method responds to the click event start debugging, see which steps are executed and what values you pass/get make sure that its what you expect.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM