简体   繁体   中英

How to remove row from datagrid in asp.net

I have a datagrid dgCompanies declare like this:

// bind the data to the datagrid
dgCompanies.PageSize = pageSize;
dgCompanies.DataSource = rdr;

Then I need to check the records inside this datagrid:

int j = 0;
foreach (DataGridItem item in dgCompanies.Items)
{
    HtmlGenericControl name = (HtmlGenericControl)item.Cells[j].FindControl("SpanTitle");   
    string drstring = name.InnerHtml.Trim();

    if (checkingfunction(drstring))
    {
       //do removing record from datagrid.
        I tried this:  item.Cells.Remove(item.Cells[j]); but the result still there, don't see anything removed!
    }

    j = j + 1;                    
 }

 dgCompanies.DataBind();

How could I remove that record from datagrid dgCompanies when if condition is satisfy ?

I agree with @Andrei, it would be best to just not return that data. Otherwise you're returning and churning through data that is unnecessary. However, if in the context of you can't filter the data up front, I suppose you could add a css class "donotshow" to your rows (see How do I specify CSS classes for specific rows in a GridView? ).

Then using jquery use

$('.donotshow').hide();

Again, in this regard, you are still returning all that HTML to the browser, so that will make your page size larger than necessary. Additionally, using this method could screw up pagination if you using it (example, if it says "rows 1-10", but 5 rows are hidden, then that will look goofy).

Hope this helps

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