Skip to main content

Microsoft Silverlight

Answered Question ComboBox seletedItem in DataRSS Feed

(0)

methis
methis

Member

Member

136 points

20 Posts

ComboBox seletedItem in Data

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 Embarrassed

newbiee

 

Elango.ka
Elango.ka

Participant

Participant

816 points

146 Posts

Answered Question

Re: ComboBox seletedItem in Data

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
shamrat231

Contributor

Contributor

4479 points

572 Posts

Answered Question

Re: ComboBox seletedItem in Data

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 in
in cbovalue.Text

Now:
1. change selected item in first row of combobox
2. chage the selected item in second row combobox
3. then choose the first row of the datagrid,
4. in selectionChangeEvent of datagrid, i will get the first row id and
5. 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 programmingSmile, 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.

Dhaka, Bangladesh
LinkedIn :: SL Profile :: Blog

methis
methis

Member

Member

136 points

20 Posts

Re: ComboBox seletedItem in Data

 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 solnSad

Anyone?? 

Thanks

newbie

shamrat231
shamrat231

Contributor

Contributor

4479 points

572 Posts

Answered Question

Re: ComboBox seletedItem in Data

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 Mahmud
Software Developer
(MCP,MCTS,MCPD[web])

This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.

Dhaka, Bangladesh
LinkedIn :: SL Profile :: Blog

methis
methis

Member

Member

136 points

20 Posts

Re: ComboBox seletedItem in Data

Thanks

basechen
basechen

Member

Member

15 points

16 Posts

Re: ComboBox seletedItem in Data

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

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities