Skip to main content

Microsoft Silverlight

Answered Question determine datagrid rowRSS Feed

(1)

slen
slen

Member

Member

10 points

59 Posts

determine datagrid row

Is there any way to know the number of row in the datagrid? Can a datagrid in silverlight to have the number of page function? Or there should have another object to do these functions?

 

Thanks

TomBeeby
TomBeeby

Participant

Participant

1151 points

188 Posts

Answered Question

Re: determine datagrid row

>> Is there any way to know the number of row in the datagrid?

The ItemsSource of a DataGrid is a collection.. try counting the number of items in the collection using Count:

 

((IList)datagrid1.ItemsSource).Count

  

or if the ItemsSource is of type IEnumerable you could use Linq's Count() method

 

>> Can a datagrid in silverlight to have the number of page function?

Yes! Have a look at a DataPager control

http://silverlight.codeplex.com/Wiki/View.aspx?title=Silverlight%20Toolkit%20Overview%20Part%205

slen
slen

Member

Member

10 points

59 Posts

Re: Re: determine datagrid row

What is the attribute that I can use to loop through datagrid? I want to detect the checkbox in datagrid.

Thanks

 

TomBeeby
TomBeeby

Participant

Participant

1151 points

188 Posts

Re: Re: determine datagrid row

think of 'looping through the collection bound to the datagrid' not 'looping through the datagrid'

so,

if you have a collection

 

IEnumerable<Project> Projects;
  

and the Project class has a bool property called IsActive that is bound to a DataGridCheckBoxColumn, like:

 

<data:DataGrid ItemsSource="{Binding Products}">
<data:DataGrid.Columns>
<data:DataGridCheckBoxColumn Binding="{Binding IsActive}" />
</data:DataGrid.Columns>
</data:DataGrid>
  

then you can run some linq on the collection, like so:

 

var res = Projects.Where(i => i.IsActive).ToList();
  

if you wanted, say, to get a list of all the active projects

slen
slen

Member

Member

10 points

59 Posts

Re: Re: Re: determine datagrid row

Can I know what the i is represented ?

I am not using the DataGridCheckBoxColum. I am using the template.

If I use the DataGridCheckBox, all the checkbox will be checked.

I am not familiar with C#.

May I know what the => pointer points to? How do I do it in VB?

Thanks

 

TomBeeby
TomBeeby

Participant

Participant

1151 points

188 Posts

Re: Re: Re: determine datagrid row

sounds like you need to brush up on linq

http://msdn.microsoft.com/en-gb/library/bb308959.aspx

Linq is 'a general purpose query facility' for the .net framework

i can't help you with VB syntax i'm afraid, though i imagine the link above will help

slen
slen

Member

Member

10 points

59 Posts

Re: Re: Re: Re: determine datagrid row

Thanks for the link Tom. Sure I will bookmark the link.

I have never really done anything with Linq before I started to use silverlight last friday

Is there another way to loop through the datagrid's collection?

Now, in my xaml file is like the following:

 <my:DataGridTemplateColumn>
                        <my:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox x:Name="rowckbx" Tag="{Binding id}"  />
                            </DataTemplate>
                        </my:DataGridTemplateColumn.CellTemplate>
  </my:DataGridTemplateColumn>

  the id that I am binding to is actually in integer form, starting from 1 and onward. Innitially, I was thinking to loop throught the datagrid, row by row, to check whether the checkbox rowckbx is checked.Now, I am kind of confused.

Any good/easy approach to do this? Loop through -> check is checked -> get the binded id -> delete data from the list

Thanks

TomBeeby
TomBeeby

Participant

Participant

1151 points

188 Posts

Re: Re: Re: Re: determine datagrid row

DataGrid supports selection out of the box. Make sure SelectionMode="Extended", then you can click on a row to select, or use CTRL and SHIFT to select multiple rows.

In your codebehind you can access the SelectedItems

so, if you want to delete items from a collection that is bound to a DataGrid, put a button on the page and in the Click handler, delete all the items in yourdatagrid.SelectedItems

slen
slen

Member

Member

10 points

59 Posts

Re: Re: Re: Re: Re: determine datagrid row

this surely works well. 

Maybe I want to know more or just I find it having a checkbox in the grid is what I really want. 

Do you have any idea to work around that? 

Having the previous example given above, how do you add another column in the itemsource to determine whether any of the  checkboxes is checked?

Thanks 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities