tags:

views:

144

answers:

3

this.entityModel.Entities is a source to my datagrid(agdatagrid).

I have kept AutoGenerateColumns="False". i have 6 columns in my agdatgrid

i want 6th column to be visible depending on the data of that column..ie., if any row of that column contains the data then it should be visible and if none of the row contains the data for that column it should be invisible.

So i have written a foreach loop but it takes more time to get ui loaded if the data is large. so is there any other way ?

foreach (BrowserEntity _browseEntity in this.entityModel.Entities)
{
            if (_browseEntity.State != null && this.entityModel.Entities.Count>0)
            {
                this.grid.DataSource = this.entityModel.Entities;
                this.grid.Columns[6].Visible = true;
                break;
            }
            else
            {

                this.grid.DataSource = this.entityModel.Entities;
                this.grid.Columns[6].Visible = false;
            }
}
A: 

I'm not a silverlight developer, but why do you check for "this.entityModel.Entities.Count>0" in the foreach loop? I would assume the count is always >0 when you enter the loop, isn't it?

Michael
i don't want to display the column even if there are no row data for that columns. So the count is 0 and it dislays the column.So to avoid this i am using this.entityModel.Entities.Count>0
Malcolm
maybe it's Silverlight specific, but the loop is over the collection of this.entityModel.Entities and within the loop you check for the numbers of elements of this collection. My understanding is that it will only go into the loop when there are actually elements, so the check for .Count>0 is superfluous.Although I might be totally wrong here.
Michael
+1  A: 

I'm not an expert on this...but why are you resetting the DataSource every time?

bool isColumnVisible = false;
this.grid.DataSource = this.entityModel.Entities;
foreach (BrowserEntity _browseEntity in this.entityModel.Entities)
{
            if (_browseEntity.State != null && this.entityModel.Entities.Count>0)
            {
                isColumnVisible = true;
                break;
            }
}
this.grid.Columns[6].Visible = isColumnVisible;

I think this should be faster...at least I hope so.

Bobby
yes it is fatser then the earlier :)
Malcolm
+2  A: 

First look at the logic of what you're writing. You're checking whether the count of a collection is greater than zero inside a loop that iterates over it; this will always return true as the loop will not run if the collection contains anything. So what you're actually writing is this, when code that either always returns true or which cannot execute is removed:

foreach (BrowserEntity _browseEntity in this.entityModel.Entities)
{
    if (_browseEntity.State != null)
    {
        this.grid.DataSource = this.entityModel.Entities;
        this.grid.Columns[6].Visible = true;
        break;
    }
}

So you're assigning the data source a number of times, and never setting Visible to false, whereas I think what you're actually trying to write is something like this:

// bind the grid but hide column 6
this.grid.DataSource = this.entityModel.Entities;
this.grid.Columns[6].Visible = false;

// if there is any state then show column 6
foreach (BrowserEntity _browseEntity in this.entityModel.Entities)
{
     if (_browseEntity.State != null)
     {
         this.grid.Columns[6].Visible = true;
         break;
     }
}

Alternatively, using Linq, this could be written as the following, which achieves the same thing but is much clearer:

this.grid.DataSource = this.entityModel.Entities;
this.grid.Columns[6].Visible = this.entityModel.Entities.Any(e => e.State != null);
Greg Beech
i will go with a linq query as far my understanding linq query will be faster then teh foreach loop.. Thanks for u r awesome answer
Malcolm