Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit DataBinding: using a listbox inside a DataGrid
12 replies. Latest Post by apple0124 on April 27, 2009.
(0)
brauliod
Participant
1161 points
467 Posts
06-27-2008 2:07 PM |
Hi,
I'm using a DataGrid to display some binded data. In order to let the user edit online the rows of the data grid I have created several CellEditingTemplates, If I use for instance a textbox for editing a column, works fine...
BUT... what happens if I want to use a listbox in that data template? How can I indicate the ItemsSource of that ListBox ? (the data binded only contains the main entity) Should I create my own entity that contains all the data necessary? (something like a crappy dataset?).
Thanks in advance
Braulio
lee_sl
Contributor
2990 points
584 Posts
06-27-2008 2:20 PM |
you should be able to set the ItemsSource in BeginningEdit event handler
06-27-2008 2:43 PM |
Thanks for the tip but... how can I retrieve from this event the instance of the listbox that is being shown?
06-27-2008 2:46 PM |
you can use GetCellContent() method to get the content of the cell, which you will cast to a listbox in your case
06-27-2008 3:00 PM |
Thanks... quite near to the solution, but...
e.Column.GetCellContent(e.Row)
Returns me the control used for display mode, Is there any other event where the grid returns me the control used in edit mode / template?
Thanks
Brauliod
06-27-2008 3:11 PM |
Hi Lee, It seems that the event to hook is: PreparingCellForEdit This event is fired after the control is loaded. Thanks a lot for your help, as soon as I get the full solution working I will post it here (then another dummy like me can perform the binding :-)).
Hi Lee,
It seems that the event to hook is:
PreparingCellForEdit
This event is fired after the control is loaded.
Thanks a lot for your help, as soon as I get the full solution working I will post it here (then another dummy like me can perform the binding :-)).
06-27-2008 3:29 PM |
you are right about the event, PreparingCellForEdit is the right one. if we move the code you wrote before to a handler for this event, it should work
06-27-2008 4:30 PM |
Yes, and this event gives some helpfull params
//You can obtain the current row datasource item from the grid
Entities.
// You can obtain the control associated to that cell
ListBox
Now I have realized, that I have the problem of having ID / Description values... I think I will have to do more manual tweaking to display in the TextBlock the Description but keep the ID internally :-(.
Thanks for your help
06-27-2008 4:39 PM |
look at DisplayMemberPath set Description to that property and when you get the selectedItem you get the complete object. you can get the id value from that. if you need more customization apply ItemTemplate to the listbox
06-27-2008 5:03 PM |
Thanks for the info, for the ListBox now is under control.
For the textblock (non editing mode) I'm going to hook on the Loaded event for that control and set there the description value (manually search for the description associated with a given ID), maybe is not very elegant.
Another possiblity could be to add to my entity ID plus description fields (something like Entity: car, color = 1 colorDesc=Red), but I would have to manually update both fields whenever there is a change.
chinna_g
Member
17 points
66 Posts
03-13-2009 1:22 AM |
I am looking for the same problem .... I am populating the data from dbase to the grid in silverlight2.0.
Now my requirement is when i select a row i need to get the updated/modified data for the particular row.... (with a hidden field like in asp.net 'DataKeyField i.e. if i select one row i need to get the EmpId for that particular row where as the empID will ot be visible to the user).
Please let me know the solution.....
obsidian3d
9 points
20 Posts
04-23-2009 6:21 PM |
I don't see the .GetCellContent method on here anymore...has it been removed?
apple0124
3 points
2 Posts
04-27-2009 7:59 PM |
SL 3.0 datagrid. I have created my CellEditingTemplate using a XAML. My GetCellContent(e.Row) gives the GridRow back not the combobox.
Here is some code
When I do
void parentGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e) { DataGrid dg = (DataGrid)sender; ComboBox cboItem= (ComboBox)dg.CurrentColumn.GetCellContent(e.Row); cboItem.ItemsSource = _pickListItems; cboItem.SelectedIndex = 0;
MessageBox.Show( e.Column.DisplayIndex.ToString()); }
GetCellContent gives me the Grid.Row.
How do I get the combobox?