简体   繁体   中英

delete row in Gridview after databind()

DataSource has been provided to gridview as shown below and it works fine

dummyGridview.DataSource = dtUser; //dtUser is DataTable
dummyGridview.DataBind();

Now without OnRowDeleting command, Is it possible to delete any particular row from Gridview based on DataKeyNames just after the Databind() ?

It will be good if you remove that particular row from your DataTable first and then bind to gridview as shown below:

for (int i = 0; i < dtUser.Rows.Count; i++)
{
    //check 
    if(dtUser.Rows[i]["DataKeyName"].ToString()) == yourValue))
    {
        //remove
        dtUser.Rows.Remove(dtUser.Rows[i]);
        break;
    }
}

then

dummyGridview.DataSource = dtUser;
dummyGridview.DataBind();

Hope It solves your purpose.

if you want to remove more than 1 row from the datatable you can use the following code:

for(int i = 0; i < dtUser.Rows.Count; i++)
{
    if (dtUser.Rows[i]["DataKeyName"] == req.value)
    {
        dtUser.Rows.Remove(dtUser.Rows[i]);
        i--;
    }
}

after that you can bind the database

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