0

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 ?

3
  • Wouldn't it be easier and more natural to filter the datasource instead? Commented May 22, 2014 at 14:41
  • I don't think it is easy, pls refer to my question here: stackoverflow.com/questions/23774851/… Commented May 22, 2014 at 14:44
  • I tried: item.Cells.Remove(item.Cells[j]); but the result still there, don't see anything removed! Commented May 23, 2014 at 3:11

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

How do I "mark" that rows in codebehind when if condition is satisfy? then how to hide it ? Could you give example with codebehind and ascx file ?
I tried this: item.Cells[j].Visible = false; but I don't see any change either.
Do I need to make change at ascx file as well ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.