Skip to main content

Microsoft Silverlight

Answered Question Selecting Rows in a DatagridRSS Feed

(0)

Darren9962
Darren9962

Member

Member

5 points

24 Posts

Selecting Rows in a Datagrid

I've got a bound  datagrid with columns like so:

            PharmacyList.Columns.Add(new DataGridCheckBoxColumn { Binding = new Binding("Selected") });
            PharmacyList.Columns.Add(new DataGridTextColumn { Header = "ID",         Binding = new Binding("PharmacyID"),   IsReadOnly = true, Visibility = Visibility.Collapsed });
            PharmacyList.Columns.Add(new DataGridTextColumn { Header = "City",       Binding = new Binding("PharmacyCity"), IsReadOnly = true });
            PharmacyList.Columns.Add(new DataGridTextColumn { Header = "Zip",        Binding = new Binding("PharmacyZIP"),  IsReadOnly = true });
            PharmacyList.Columns.Add(new DataGridTextColumn { Header = "Name",       Binding = new Binding("PharmacyName"), IsReadOnly = true });
            PharmacyList.Columns.Add(new DataGridTextColumn { Header = "Address",    Binding = new Binding("PharmacyAddr"), IsReadOnly = true });
            PharmacyList.Columns.Add(new DataGridTextColumn { Header = "Fax Number", Binding = new Binding("FaxNumber"),    IsReadOnly = true });

 I'd like to update the "Selected" field when the row is selected.  I've looked all over and haven't found a good solution.  I don't see any events that I can use.  Also I'd like for the first row not to be selected automatically when the datagrid is loaded.

msalsbery
msalsbery

Participant

Participant

1942 points

358 Posts

Re: Selecting Rows in a Datagrid

I'd like to update the "Selected" field when the row is selected. I've looked all over and haven't found a good solution. I don't see any events that I can use.

 

Perhaps the DataGrid.SelectionChanged event?

Mark

 

Mark Salsbery
Microsoft MVP - Visual C++

yifung
yifung

Contributor

Contributor

3313 points

540 Posts

Microsoft
Answered Question

Re: Selecting Rows in a Datagrid

Is Selected a property on your entity, and you have one for each object in your list?  If so, make sure your entity implementd INotifyPropertyChanged.  Use the SelectionChanged event on the DataGrid as suggested above.  When you get the event, loop through e.OldItems and set Selected to false and do the same for e.NewItems but set Selected to true on those.  The CheckBoxes in your UI will automatically update since they're databound

Yifung Lin [MSFT]
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities