Skip to main content
Home Forums General Silverlight Getting Started ComboBox seletedItem in Data
6 replies. Latest Post by basechen on May 27, 2009.
(0)
methis
Member
136 points
20 Posts
12-31-2008 10:14 PM |
Hi, all any time in this post would be
I am actually new with this. Here is my template column
<data:DataGridTemplateColumn Header="destination"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox x:Name="cbo" DisplayMemberPath="Name" ItemsSource="{Binding Country}"/> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn>
ok, now i have a datagrid where first column gives ID, and second column is this Combobox [for each row]. Suppose the user choose one of the item in combobox and then select the row in the datagrid, now on selectionChangedEvent in datagrid,
1. how would i actually findout the combobox in the selected row,
so i can get the item selected in combobox. i hope the code solution is simple
private void dataGrid_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
//what is it that i have to do here to get the combo of the selected row
}
I couldnot find any article where they actually find the item selected in combobox when a row is selected in DataGrid.
Thanks. some code base solution would help
newbiee
Elango.ka
Participant
816 points
146 Posts
01-01-2009 7:40 AM |
Hi,
The datagrid selection changed event is based by the datagrid events, not a event for combo box. so, you can add the selectionchanged event at your datagrid combobox.
<data:DataGridTemplateColumn Header="destination"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox x:Name="cbo" DisplayMemberPath="Name" ItemsSource="{Binding Country}" SelectionChanged="cboChanged"/> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn>
If you need which row combo box is selected, then add and use with DataContext property,
<ComboBox x:Name="cbo" DisplayMemberPath="Name" ItemsSource="{Binding Country}" SelectionChanged="cboChanged" DataContext="PrimaryId"/>
Now your cboChanged event is,
private sub cboChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
Dim cmbobx as new combobox
combobox=sender
id=combobox.datacontext
country=combobox.selecteditem.tostring
end sub
Thanks
shamrat231
Contributor
4479 points
572 Posts
01-03-2009 2:20 AM |
Hi, Elango.ka
i tried your scenario and it failed the first test case i wrote based on the soln that you provided
suppose in combobox selectionChangeEvent if i save the result inin cbovalue.Text
Now:1. change selected item in first row of combobox2. chage the selected item in second row combobox3. then choose the first row of the datagrid,4. in selectionChangeEvent of datagrid, i will get the first row id and5. in cbovalue.Text the combobox[int this case second row selection] as it was selected last in cbo selectionChangedEvent
[CONC] this will not work, and is not good programming, i am having the same problem
Hopefully someone would be able to give a better soln
and if this post was helpful then please 'Mark as Answer' - many thanks
Sharker Khaleed Mahmud (MCP,MCTS,MCPD[web])Software Developer
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
01-03-2009 10:11 PM |
The answer provided by Elango.ka is not good enough and yes, shamrat231 is right. This itself is frustating.
in ASP.NET it could easily be done by this way
string slc = ((ComboBox)GridView1.SelectedRow.FindControl("cbo")).SelectedItem.Text for each row in the grid. in selectionEventChanged in GridView
Can anyone could give a good/strong soln
Anyone??
newbie
01-07-2009 3:37 AM |
methis: string slc = ((ComboBox)GridView1.SelectedRow.FindControl("cbo")).SelectedItem.Text for each row in the grid. in selectionEventChanged in GridView Anyone??
Here is the soln
ComboBox cbo = (ComboBox)DataGrid.Columns[1].GetCellContent(DataGrid.SelectedItem);
string slc = ((PersonClass)cbo.SelectedItem).Name;
where
class Person
public string Name {get;set;};
and if the post was helpful then please 'Mark as Answer' - many thanks
Sharker Khaleed MahmudSoftware Developer(MCP,MCTS,MCPD[web])
01-07-2009 3:44 AM |
basechen
15 points
16 Posts
05-27-2009 7:19 AM |
Hi, I would like to know how can i fully code it with C# include the DataGridTemplateColumn??
I got the solution, just ignore this.
http://blogs.msdn.com/scmorris/archive/2008/04/14/defining-silverlight-datagrid-columns-at-runtime.aspx