Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit determine datagrid row
8 replies. Latest Post by slen on July 5, 2009.
(1)
slen
Member
10 points
59 Posts
06-30-2009 9:25 PM |
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
Participant
1151 points
188 Posts
07-01-2009 2:02 AM |
>> 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
07-02-2009 4:28 AM |
What is the attribute that I can use to loop through datagrid? I want to detect the checkbox in datagrid.
07-02-2009 8:51 PM |
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
07-02-2009 10:33 PM |
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?
07-02-2009 11:04 PM |
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
07-02-2009 11:32 PM |
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
07-03-2009 1:03 AM |
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
07-05-2009 10:20 PM |
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?